I am creating the cell programatically using the reuse identifier.
Note – I am not using storyboard for creating the cell
Whenever the cell is dequeued, the cell is nil, so the cell needs to be newly created using alloc, which is expensive.
EDIT (added 1 more question and corrected code)
Question
- Why does this dequeue always return nil ? How can I correct it ?
- Does dequeue work only when used along with storyboard / nib file ?
Code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell) //Every time cell is nil, dequeue not working
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
I was making a couple of mistakes:
UITableViewController, but was creating the tableView outside of the subclasstableViewcreated in the table view controller, which isself.tableViewIn the tableview controller while returning the cell for index path, I was usingself.tableViewinstead oftableView.Also, ensure that the cell identifier is declared as
staticSince
tableViewandself.tableViewwere representing different tables, the cell was not being dequeued from the same table and hence was alwaysnil