iosiphoneswiftuitableviewdidselectrowatindexpath

Manually call didDeselectRowAtIndexPath when cell is created


I have a UITableViewinside of a UIViewController and the UIViewController is the delegate for the UITableView. In my table view I am outputting a variable number of sections with a variable number of rows. Whenever any cell is selected, a checkmark needs to appear next to it (which is currently working). However, a specific section needs checkboxes next to it when the cells are created.

To account for this I have an array with headers, data, and a flag.

Here is my array which specifies the staticFlag

{arrayOfHeadersAndData.append((/// arrray stuff in a tuple ///,staticFlag: 0))}

Here is how I add the checkboxes:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
     tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = UITableViewCellAccessoryType.Checkmark
}

And here is how I am trying to add a checkbox on the section which has a staticFlag=0=

if arrayOfHeadersAndData[indexPath.section].staticFlag == 0 {
        tableView(tableViewOfCurrentItems, didSelectRowAtIndexPath: indexPath)
}

My current error is

"Cannot call value of non-function type 'UITableView'


Solution

  • tableView:didSelectRowAtIndexPath: is a UITableViewDelegate method and needs to be called on a UITableViewDelegate instance e.g. self.tableView(tableViewOfCurrentItems, didSelectRowAtIndexPath: indexPath) or tableView.delegate?.tableView(tableViewOfCurrentItems, didSelectRowAtIndexPath: indexPath).

    It seems odd that you would call didSelectRowAtIndexPath to apply this change. That sounds like the delegate is dictating details of the appearance of the table view cells. Instead you might implement the display of this checkmark entirely within a custom UITableViewCell subclass and have it manage hiding or showing the checkmark in response to it's selected property and setSelected:animated: method. Then you could just call cell.selected = true when preparing preselected cells. That approach would also allow you to implement prepareForReuse on your cell class to clear the selection state as needed before cells are reused and avoid potentially showing these checkmarks on other rows unintentionally.