Custom UITableViewCell using Storyboard

江枫思渺然 提交于 2019-12-12 13:01:10

问题


I'm trying to make a custom cell using storyboard. I have tested my program with Basic cells and it worked. Now I'have created a new class that I named NewsCell, which is containing the different Labels in the custom cell. I have also made the cell a subclass of NewsCell. The cell identifier is "NewsCell".

This is the cellForRowAtIndexPath: method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NewsCell *cell = (NewsCell *)[tableView dequeueReusableCellWithIdentifier:@"NewsCell"]; //signal : SIGABRT
    News *info = [self.news objectAtIndex:indexPath.row];
    cell.titreLabel.text = info.titre;
    cell.descriptionLabel.text = info.description;
    return cell;
}

When I run my app, it crashes with a signal signal SIGABRT at the first ligne. I'm sure that I've made the right connections between Labels and the table view cell.

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'The NIB data is invalid.'


回答1:


I had the same problem. It turned out that I had checked "Use Autolayout" in the Interface Builder Document pane of the Storyboard file. This throws the "invalid NIB" error when running on iOS 5 because autolayout is only supported on iOS 6.




回答2:


You have not created instance for the cell. Try to create instance for the cell.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    NewsCell *cell = (NewsCell *)[tableView dequeueReusableCellWithIdentifier:@"NewsCell"]; //signal : SIGABRT

     if (cell == nil) {
            cell = [[[NewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
    News *info = [self.news objectAtIndex:indexPath.row];
    cell.titreLabel.text = info.titre;
    cell.descriptionLabel.text = info.description;
    return cell;
}


来源:https://stackoverflow.com/questions/12579844/custom-uitableviewcell-using-storyboard

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