dmur
dmur

Reputation: 530

Determine if UITableView has static cells or dynamic prototypes programmatically?

I'm writing an abstract UITableViewController class and I'd like to write something in viewDidLoad like

if (self.tableView.contentType == UITableViewContentTypeStaticCells) {
    // Do something when table view has static cells
} else {
    // Do something when table view has dynamic prototypes
}

But obviously there is no contentType on UITableView. Is there a way to determine programmatically whether the tableView's storyboard content is static or dynamic?

Upvotes: 6

Views: 800

Answers (3)

Enrico Cupellini
Enrico Cupellini

Reputation: 445

my solution assumes the abstract UITableViewController class must expose a BOOL property

@property (assign, nonatomic) BOOL staticCells;

this property is valorised by the concrete classes, and the datasource methods implementation checks the property's existence condition like in this case:

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.staticCells) {
        ...
    }
    else{
        UITableViewCell* cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
        [cell layoutIfNeeded];
        return cell;
    }
}

I suppose you were looking for a system framework property (or delegate method) to check the static behaviour but maybe this solution can be useful for someone

Upvotes: 0

osxdirk
osxdirk

Reputation: 560

Just for the curious: [tableViewController valueForKey: @"staticDataSource"] will get you there, where tableViewController is a UITableViewController.

BUT(!) this might not pass the AppStore and may break without warning as it's not published API.

Update: It seems that checking if checking, if

self == self.tableView.dataSource

while self is a UITableViewController also gives you re requested result.

Upvotes: 1

Mick MacCallum
Mick MacCallum

Reputation: 130222

There's no build in way to distinguish between the two, but if you're more specific about what you're trying to achieve, we may be able to suggest alternative ways of accomplishing your goal.

Upvotes: 0

Related Questions