30

I have a static UITableView and everything is laid out on the Storyboard. In cellForRowAtIndexPath i simply use

return [super tableView:tableView cellForRowAtIndexPath:indexPath];

and the tableview works great. However I want to set the cells backgrounds to [UIColor clearColor] How can I do this? The table has 30 cells, each one different.

Thanks

Darren
  • 10,182
  • 20
  • 95
  • 162

3 Answers3

64

I'm not entirely sure if this will work, because I've never tried using super to manage the cells. If your code works with super, then you should be able to get away with doing this to set the background color:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

Either way, this seems to me like a strange way of doing things. Normally the cells are created and reused in the method itself, as per the default UITableView boiletplate code, without calling super.

Jonathan Ellis
  • 5,221
  • 2
  • 36
  • 53
  • 2
    Thanks, this worked great. As the table is very long and every cell is completely different and static, I found it easier to just set them all in Storyboard and create IBOutlet's to all UILabels that needed data. – Darren Aug 16 '12 at 19:54
22

Try this delegate method:

- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
 cell.backgroundColor = [UIColor clearColor];
}
CSmith
  • 13,318
  • 3
  • 39
  • 42
  • This method did work, thanks. However I selected jon's answer to be the correct one as it seemed the correct place to set the cell. Thanks – Darren Aug 16 '12 at 19:52
  • 1
    The accepted answer works but is unnecessarily verbose and complex. `willDisplayCell:` is far more straightforward and perfectly suited for the use case. – Murray Sagal Sep 29 '14 at 02:06
1

You can set up a method and call it in your viewDidLoad in order to get all the cells and do whatever you want with them you can use this:

NSArray *visibleIndexPaths = [self.tableView indexPathsForVisibleRows];

To get all the index paths of the currently viewable cells

Then iterate over them in the following way

UITableViewCell *cell = nil;
for (int i = 0; i < 30; i++) {
  cell = [self.tableView cellForRowAtIndexPath:[visibleIndexPaths objectAtIndex:i]];
  // Do your setup
}

You can also reference this Apple developer document the part "The Technique for Static Row Content"

8vius
  • 5,786
  • 14
  • 74
  • 136