【发布时间】:2016-04-06 16:04:54
【问题描述】:
我想做一个应用程序,比如一个带有 tableview 的待办事项应用程序。当我点击添加按钮(由self.navigationItem.rightBarButtonItem = addButton 提供的系统)时,会出现一个新的tableview 和一个包含给定项目的列表(由coreData 的实体提供)。
现在我想通过触摸一个单元格来设置一个复选标记,最后我想按下“保存”按钮。用于在第一个 tableview 中获取所有选定(选中的项目)。
我想在didSelectRowAtIndexPath 中进行。
但是当我上下滚动时,即使在我没有检查的单元格中也会得到复选标记。然后我尝试了以下代码:
在cellForRowAtIndexPath
cell.detailObejct = (dataOfEntity[indexPath.row])
if checked[indexPath.row] == false {
cell.accessoryType = .None
}
else if checked[indexPath.row] == true {
cell.accessoryType = .Checkmark
}
在
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
if cell.accessoryType == .Checkmark
{
cell.accessoryType = .None
checked[indexPath.row] = false
}
else
{
cell.accessoryType = .Checkmark
checked[indexPath.row] = true
}
}
但是当我运行该代码时,我收到一个错误,指出数组索引超出范围。数组声明为
var checked = [Bool]()
【问题讨论】:
-
你在什么时候让索引越界崩溃??
标签: ios swift uitableview tableviewcell