Reputation: 1401
I try to reuse UITableViewCell as follow:
1) Create subclass by UITableViewCell with xib-file.
@interface BaseCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *labelTitle;
@end
2) Add reuse identifier in IB
3) Add UIViewController with UITableView in it.
@interface BaseTableViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
4) in viewDidLoad register cell nib
[self.tableView registerNib:[UINib nibWithNibName:@"BaseCell" bundle:nil] forCellReuseIdentifier:@"BaseCell"];
5) and use dequeueReusableCellWithIdentifier
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BaseCell* cell = [tableView dequeueReusableCellWithIdentifier:@"BaseCell"];
NSAssert(cell, @"cell registered but not created");
DLog(@"cell:[%p]", cell);
cell.labelTitle.text = self.showItems[indexPath.row];
return cell;
}
After that I see that cell created every time instead only first time.
My log for example:
[0x7f87c9c84740:1] -[BaseCell initWithCoder:] BaseCell
[0x7f87c9c84740:1] -[BaseCell awakeFromNib] reuseIdentifier:BaseCell;
[0x7f87c9d3d230:1] -[BaseTableViewController tableView:cellForRowAtIndexPath:] cell:[0x7f87c9c84740]
[0x7f87c9e29560:1] -[BaseCell initWithCoder:] BaseCell
[0x7f87c9e29560:1] -[BaseCell awakeFromNib] reuseIdentifier:BaseCell;
[0x7f87c9d3d230:1] -[BaseTableViewController tableView:cellForRowAtIndexPath:] cell:[0x7f87c9e29560]
[0x7f87c9e2d150:1] -[BaseCell initWithCoder:] BaseCell
What I do wrong? How properly reuse cells.
I use latest Xcode (6.0.1)
I created example project to test it.
Upvotes: 0
Views: 730
Reputation: 21536
The benefits of cell reuse will only become apparent once cells go off-screen when scrolling (or become "unused" for some other reason - e.g. a reloadCells call). The cell that disappears is put into the queue. When the tableView next needs a cell, it uses the one on the queue, rather than constructing a new one from the NIB.
Upvotes: 0