UITableviewCell accessoryView 无法更改

UITableviewCell accessoryView can't be changed

我需要将 accessoryView 更改为正确的 UIImageView

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

     var cell = tableView .dequeueReusableCellWithIdentifier("ActivitySectionCell") as! ActivitySectionCell
     var imageNameOfAccessory = expandedSections.containsIndex(indexPath.section) ? "collapse" : "edit"
     cell.accessoryView = UIImageView(image: UIImage(named: imageNameOfAccessory))
     return cell
}


func tableView(tableViewMethod: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
            tableViewMethod.deselectRowAtIndexPath(indexPath, animated: true)

               var cell = tableView(self.tableViewMain, cellForRowAtIndexPath: indexPath) as! ActivitySectionCell
               if currentlyExpanded {
                    var imageNameOfAccessory = !currentlyExpanded ? "collapse" : "edit"
                    cell.accessoryView = UIImageView(image: UIImage(named: imageNameOfAccessory))
                }
                else {
                    var imageNameOfAccessory = !currentlyExpanded ? "collapse" : "edit"
                    cell.accessoryView = UIImageView(image: UIImage(named: imageNameOfAccessory))
                }


    }

我检查了调试,有时我设置 "collapse" 图片,有时 "edit" 图片,但之后我总是在单元格中看到 "edit" 图片。

所以我一开始就设置了accessoryView,之后就不能改了(实际上我可以改视图指针,但是之后在屏幕上我看不到任何变化)

我认为您可能需要在 cellForRowAtIndex 中执行 if currentlyExpanded { 检查,然后在 didSelectRowAtIndex 中调用 self.tableView.reloadData

我找到了解决方案。问题在行中:

var cell = tableView(self.tableViewMain, cellForRowAtIndexPath: indexPath) as! ActivitySectionCell

我不得不将其更改为:

var cell = tableViewMain.cellForRowAtIndexPath(indexPath) as! ActivitySectionCell

否则我不会更改当前单元格,而是其他返回单元格