简体   繁体   中英

Custom Cell UITableview

I want to have a custom cell in my UITableview. I have tried it with the following:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {

    NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:nil options:nil];

    for (UIView *view in views) {
        if([view isKindOfClass:[UITableViewCell class]])
        {
            cell = (CustomCell*)view;
        }
    }
}
[cell setLabelText:[daten objectAtIndex:indexPath.row]];
return cell;

}

But I am getting an exception on that line:

CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

This is how I made my custom cell and called it from the class :-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     {
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:[cell reuseIdentifier]];

    if (cell == nil)
    {
        [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = customCell;
        customCell = nil;
    }

    // Configure the cell.
    cell.serialLabel.text = [[NSNumber numberWithInt:indexPath.row+1]stringValue];
    cell.textLabel.textColor=[UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    }

I'm almost sure that you have lost connection in your XIB file. If commentLabel is still exists you should either delete it and create a new outlet or check the outlets section and delete extra connection and all will be alright!!

EDIT: This error occurs when you have two outlets connected to the same label in IB.

在此处输入图片说明

if u are not using the latest ios SDK, make sure that u have written @synthesize commentLabel; in CustomCell

尝试清理构建文件夹,或尝试模拟器->重置内容和设置。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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