Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters to custom UITableViewCell [duplicate]

I'm registering a custom cell from a Nib in the TableViewController

tableView.registerNib(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "Cell")

but my CustomCell has IBAction methods that need access to the tableView itself and the fetchedResultsController. How do I pass those parameters in a clean way to the CustomCell?

Setting those things in:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) { 
   return tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 
}

doesn't seem to be the clean way to me..?

like image 746
NSCode Avatar asked Apr 16 '26 18:04

NSCode


1 Answers

The cell really shouldn't know anything about the table view or the fetchedResultsController. One way to do this is to add your actions in cellForRowAtIndexPath, and make the controller the target.

Another option is to have the action methods in the custom cell class, and have them call delegate protocol methods that you create in the cell class. Have the controller set itself as the delegate in cellForRowAtIndexPath.

like image 152
rdelmar Avatar answered Apr 18 '26 07:04

rdelmar