【问题标题】:Custom tableview(cell) with checkmark带有复选标记的自定义表格视图(单元格)
【发布时间】: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


【解决方案1】:

感谢@dsieczko,我更改了代码中的一些细节:

cell.detailObejct = (dataOfEntity[indexPath.row])
cell.accessoryType = .None // THIS IS MANDATORY
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 // this line removes now the entry instead of setting it only to false
checked[indexPath.row] = nil
        }
        else
        {
            cell.accessoryType = .Checkmark
            checked[indexPath.row] = true
        }
}

当然是字典:

var checked = Dictionary<Int, Bool>()

【讨论】:

    【解决方案2】:

    知道了,您没有向已检查的数组添加任何元素,我的意思是它有 0 个元素,但您正试图在数据源方法中取出一个项目,因此使用所有错误值初始化已检查的数组,因为最初没有选择任何行,

    for _ in 0...tableItems.count{ //tableItems should be your data source count
      checked.append(false)
    }
    

    我尝试了同样的方法,一切正常,在 viewDidLoad() 中添加上述语句

    【讨论】:

      【解决方案3】:

      您似乎没有考虑原始数据源的大小。

      你要么需要为你检查的变量实例化一个字典,

      var checked = Dictionary<Int, Bool>()
      

      跟踪原始数据源中的索引。

      或者实例化一个具有默认大小和值的数组

      var checked : Array<Bool>!
      
      override func viewDidLoad() {
          // You need the size of your data at this point
          checked = [Bool](count: dataSource.count, repeatedValue: false)
      }
      

      如果还有什么我可以帮忙的,请告诉我。

      【讨论】:

      • 现在可以了。我不得不更改一些代码,例如如果复选标记设置为不设置为false,则我删除了dict中的项目。但是你设置一个字典而不是设置一个数组的后裔成功了。
      • 太棒了!很高兴我能提供帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多