简体   繁体   中英

UITableviewCell accessoryView can't be changed

I need to change the accessoryView to proper 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))
                }


    }

I checked in debug, sometimes I set "collapse" image, sometimes "edit" image, but after that I always see "edit" image in the cell.

So when I set accessoryView at the very beginning , I can't change it after that (actually I can change the view pointer, but after that on the screen I don't see any changes)

我认为您可能必须执行if currentlyExpanded { cellForRowAtIndex if currentlyExpanded {检查cellForRowAtIndex并仅在didSelectRowAtIndex调用self.tableView.reloadData

I found the solution. The problem was in the row:

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

I had to change it to :

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

Because otherwise I will change not the current cell, but other returning cell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM