Reputation: 5795
Nowadays we have to use this code somewhere in ViewDidLoad -
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];
and I am not sure what was wrong with old way by checking if cell was actually returned by queue in old method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
What confuses me, is why use some arbitary method calls in irrelevant places, and what does this "register" method does anyway?
Upvotes: 0
Views: 94
Reputation: 3043
It's since in iOS 5 that Apple introduced a short-cut method of instantiating cells from NIB files that has three stages:
Registering the NIB object needs to happen only once during the lifetime of the controller, so an obvious place to put the code is in the viewDidLoad
method of the tableView’s controller:
cellIdentifier = @"CustomCell";
[tableView registerNib:[UINib nibWithNibName:@"customCell" bundle:nil]
This takes two parameters:
nibWithNibName
methodNSString
cell identifier that was previously createdAfter the NIB is registered for use as a cell, the dequeueReusableCellWithIdentifier
method will do one of two things:
dequeueReusableCellWithIdentifier
will create one from the
registered NIB.Both of these things take place behind the scenes, so there’s now no longer any need to do the check for the cell’s existence manually. dequeueReusableCellWithIdentifier
will handle all that for you.
Upvotes: 2