简体   繁体   English

为什么我的UITableViewCell指示空值

[英]Why is my UITableViewCell indicating a null value

I am trying to get a UITableViewCell to use to change its title and contents. 我正在尝试获取UITableViewCell来更改其标题和内容。 However, I'm having trouble getting the tableViewCell because when I call the following method 但是,我在获取tableViewCell时遇到了麻烦,因为当我调用以下方法时

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

inside of this method: 此方法的内部:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 3
    static NSString *CellIdentifier = @"Flickr Photo Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // Why is cell NULL?
    if(cell == nil){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

I can see in the debugger after setting a breakpoint that the value is (null): 设置断点后,可以在调试器中看到该值为(null):

单元标识符

And in my storyboard I set the cell identifier to be Flickr Photo Cell 在我的情节提要中,将单元格标识符设置为Flickr Photo Cell

Did you register a nib or cell class with that identifier before attempting to dequeue the cell? 在尝试将单元出队之前,您是否已使用该标识符注册笔尖或单元类? Registration is usually done in viewDidLoad : 注册通常在viewDidLoad完成:

- (void)viewDidLoad {
    [super viewDidLoad];
    UINib *cellNib = [UINib nibWithNibName:@"NameOfFlickrPhotoCellNib" bundle:nil];
    [self.tableView registerNib:cellNib forCellReuseIdentifier:@"Flickr Photo Cell"];
}

you can create you cell by code: 您可以通过代码创建单元格:

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 3
static NSString *CellIdentifier = @"Flickr Photo Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
// Why is cell NULL?
if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//here configure you cel..Be carefully this cell doen't anything of you storyboard cell.
NSLog(@"Cell is not nil:%@",[cell description]);
return cell;
}

or using Storyboard (or xib) 或使用情节提要(或xib)

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseINSTORYBOARD" forIndexPath:indexPath];

// Configure the cell...

return cell;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM