dequeueReusableCellWithIdentifier always returns nil (not using storyboard)

扶醉桌前 提交于 2019-12-21 09:25:23

问题


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;
}

回答1:


You need to first set the CellIdentifier as Cell. Are you doing that? When you are creating a new cell you need to assign this identifier Cell to it. only then iOS will be able to dequeueReusableCellWithIdentifier with that identifier. Programatically you can do it like so -

UITableViewCell *cell = [[UItableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"];

You can set identifier from Interface Builder too -




回答2:


I was making a couple of mistakes:

  1. I was using a subclass of UITableViewController, but was creating the tableView outside of the subclass
  2. There is a tableView created in the table view controller, which is self.tableView In the tableview controller while returning the cell for index path, I was using self.tableView instead of tableView.
  3. Also, ensure that the cell identifier is declared as static

    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    

Since tableView and self.tableView were representing different tables, the cell was not being dequeued from the same table and hence was always nil




回答3:


This code should be generating the warning "control reaches end of non-void function" because you aren't actually returning anything. Add return cell; to the end of the function. Additionally, you never add the reuse identifier to the newly created cells.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";    
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    return cell;
}



回答4:


First declare cell identifier for a tableViewCell at the viewDidLoad method as:

[tableView registerClass:UITableViewCell.class forCellReuseIdentifier:@"MyCell"];

Now recall the instance of the UITableViewCell with the same identifier "MyCell" as:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];

Further just fill up the cell .. Now logic executes that limited number of cells are able to show enormously large list efficiently (using dequeue concept).

But remember to assign value (even nil if required) to every UIView used in the cell, otherwise overwriting / overlapping of text / images will happen.



来源:https://stackoverflow.com/questions/13379547/dequeuereusablecellwithidentifier-always-returns-nil-not-using-storyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!