0

我有一个表格视图,我需要以某种方式保存之前检查过的那些。我已经保存了那些在数组中检查的索引路径。现在我有下面的代码,我不知道如何定义“单元格”(项目是 NSindexpath )。我已经尝试过了,但它返回 nill 并且不起作用:let cell = super.tableView.cellForRowAtIndexPath(items)

override func viewWillAppear(animated: Bool) {

    for items in indexpatharray{
        if !indexpatharray.isEmpty{
        let cell = 

        cell!.accessoryType = UITableViewCellAccessoryType.Checkmark;
        cell!.selectionStyle = .None


    }

我已经尝试过了,但它返回 nill 并且不起作用:let cell = super.tableView.cellForRowAtIndexPath(items)

4

3 回答 3

1

如果您需要访问单元格,可以尝试以下操作:

let cell = tableView.cellForRowAtIndexPath(indexPath)
于 2016-07-04T04:11:29.683 回答
0

最好使用数据而不是单元格,一旦单元格被标记,你应该标记单元格后面的数据。当你需要知道被标记的时候,你寻找数据而不是单元格。

于 2016-07-04T02:55:43.010 回答
0

另外,我不确定您是否可以在 ViewWillAppear() 中调用这些委托和数据源方法,但您不应该这样做。
如果你说你有一个保存的 indexPaths 数组,那么你可以做的是 - 将情节提要中原型单元格的附件类型设置为无。这会将所有单元格的默认附件设置为无。

在 cellForRowAtIndexPath 的 dataSource 方法中,将您的单元格定义为

`let cell = tableView.cellForRowAtIndexPath(indexPath)

if(array.contains(indexPath.row)){
   cell!.accessoryType = UITableViewCellAccessoryType.Checkmark;
}
`

在 didSelectRowAtIndexPath 的委托方法中(用于 - 这将存储您点击的单元格的 indexPaths 并在其附件类型中添加复选标记。仅当该特定 indexPath 不存在时才会添加到数组中。)

{
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    if(!(array.contains(indexPath.row))){
       cell!.accessoryType = UITableViewCellAccessoryType.Checkmark;
       array.append(indexPath.row)
    } else { //to flip the state from Checkmark to None
       cell!.accessoryType = UITableViewCellAccessoryType.None;
    }  
    tableView.deselectRowAtIndexPath(indexPath, animated: true)     
}

注意:仅当您不处理大型数据集时,这是一种很好的方法。如果你是,那么我建议你查看 Core Data 和 NSFetchResultsController。

于 2016-07-04T06:04:12.853 回答