Reputation: 587
I have a UITableView
inside 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'
Upvotes: 0
Views: 1647
Reputation: 17958
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.
Upvotes: 3
Reputation: 82
Just a slight setup change. If you have an array of indexes that need checkmarks, which looks to be arrayOfHeadersAndData
, you'll want to set your checkmarks in your cellForRowAtIndexPath
method.
You'll need to reload upon adding a new cell. And your new cell will call cellForRowAtIndexPath
and get a checkmark added. So..
cellForRowAtIndexPath
, you will check the cells against arrayOfHeadersAndData
. If they are in the specific section, give them a checkmarktableView.reloadSections(NSIndexSet(index: your_section_number), withRowAnimation: .None)
Upvotes: 0
Reputation: 1205
Because you calling method as a function. The second thing is that you are trying to call method from table view but as I understood that is delegate method(and your controller is delegate). So you need to call like this :
self.tableView(tableViewOfCurrentItems, didSelectRowAtIndexPath: indexPath)
Upvotes: 1