Fonix Fonix - 11 months ago 66
iOS Question

UITableViewController accessing static cells programatically issue

Say I have a table with 10 static cells in it, is there a way to select a certain cell programmatically?

I've tried this

UITableViewCell *cell = [self.tableView.subviews objectAtIndex:indexPath.row];

but that does not actually return a table cell it seems.

this seems to crash my code

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

Im trying to set the individual heights for the static cells in code. An option would be to make outlets for each individual static cell, but that seems silly.

Answer Source

To access statically created cells, try this:

UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

This works for static cells. So, if you're in the...

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath {

     UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

    return cell;


... delegate, you can access all statically configured cells using the above declaration. From there, you can do what ever you want with "cell".

I had a ViewController that had two UITableViews on it. One of them had cells defined statically, with a Storyboard, and the other had cells defined dynamically using code. Given I was using the same ViewController as delegate for both tables, I needed to prevent new cells from being created where cellForRowAtIndexPath was being called where cells had already been created.

In your case, you need to gain programmatic access to your cells.

Have fun.