Search code examples
iosiphoneswiftuitableviewuiaccessoryview

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)


Solution

  • 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