如何在我的函数中通过索引路径访问tableviewcell

how to access tableviewcell by indexpath in my function

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

override func viewWillAppear(animated: Bool) {

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

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


    }

我已经试过了,但它 returns 无效而且不起作用: 让单元格 = super.tableView.cellForRowAtIndexPath(项目)

最好用数据而不是单元格,一旦单元格被标记,你应该标记cell.And后面的数据当你需要知道哪个被标记时,你找数据而不是单元格.

如果您需要访问单元格,可以试试这个:

let cell = tableView.cellForRowAtIndexPath(indexPath)

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

在 cellForRowAtIndexPath 的数据源方法中,将您的单元格定义为

`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。