Ossir
Ossir

Reputation: 3145

When subclassing UITableView how to customize cell preparation

I want to customize a cell in my UITableView subclass. But I cannot figure out is there any way to do it without defining itself dataSource, because it's obviously override external dataSource.

So basically I want to be UITableView dataSource without rewriting this property.

I have already come up with some dirty workaround. I'm reloading the -setDataSource: method to keep UITableView dataSource itself and save incoming data source into an internal variable for passing the requests to it.

Upvotes: 0

Views: 89

Answers (2)

Justin Holman
Justin Holman

Reputation: 852

You can customize your cell in one of two ways: code or interface builder. The tableview datasource is the DATA you want to show, it has nothing to do with the presentation of that data.

What do you want to customize about your tableview? Changing fonts, font colors and background colors are easy to do. If you want to add additional ui elements, like images, more labels, then the quickest way is to use Interface Builder.

Upvotes: 0

Fruity Geek
Fruity Geek

Reputation: 7381

You only need to override cellForRowAtIndexPath: and make your cell modifications there. The datasource will populate the cell as usual.

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Calling super will let the datasource methods be called
    UITableViewCell * cell = [super cellForRowAtIndexPath:indexPath];

    //Do whatever to the cell here
    return cell;
}

Upvotes: 1

Related Questions