TableView中的自定义单元格:无法从其数据源获取单元格

时间:2016-10-29 03:02:15

标签: ios objective-c uitableview

我正在使用以下代码在表格视图中使用两个不同的自定义表格单元格。一切看起来都正确,但是当我运行应用程序时,我收到以下错误:

  

无法从其dataSource获取单元格   FullMessageViewController:0x103d61c90

知道为什么会这样吗?

FullMessageViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];


   static NSString *ChatTableIdentifier = @"ChatTableViewCell";
   static NSString *ChatTableIdentifier2 = @"SwapDetailTableViewCell";


    UINib *nib = [UINib nibWithNibName: ChatTableIdentifier bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier: ChatTableIdentifier];

    UINib *nib2 = [UINib nibWithNibName: ChatTableIdentifier2 bundle:nil];
    [self.tableView registerNib:nib2 forCellReuseIdentifier: ChatTableIdentifier2];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

   static NSString *ChatTableIdentifier = @"ChatTableViewCell";
   static NSString *ChatTableIdentifier2 = @"SwapDetailTableViewCell";


    NSDictionary *data = [self.messages objectAtIndex:indexPath.row];


   if (![data objectForKey:@"field_swaptime"]) {

     NSLog(@"THIS IS DATA %@", data);



       ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier forIndexPath:indexPath];


       NSString *userName = [data objectForKey:@"name"];
       [cell.sendingUser setText:userName];

       NSString *messageBody = [data objectForKey:@"body"];
       [cell.messageDisplayed setText:messageBody];

       NSString *timeReceived = [data objectForKey:@"published at"];
       NSLog(@"Message Received at %@", timeReceived);
       [cell.timeStamp setText:timeReceived];


     return cell;

              }

    else {


        SwapDetailTableViewCell *cell = (SwapDetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier2 forIndexPath:indexPath];



        NSString *Time = [data objectForKey:@"field_swaptime"];
        NSLog(@"This is time %@", Time);
        [cell.startTime setText:Time];

        NSString *TimeEnd = [data objectForKey:@"field_endswaptime"];
        [cell.endTime setText:TimeEnd];

        return cell;

    }

    }

2 个答案:

答案 0 :(得分:1)

在viewDidLoad上查看此行

[self.tableView registerNib:nib2 forCellReuseIdentifier:@"ChatTableIdentifier2"];

您正在注册一个标识为ChatTableIdentifier2的nib,但您实际上并未在cellForRowAtIndexPath中使用该标识符,而是使用SwapDetailTableViewCell

将该行更改为

[self.tableView registerNib:nib2 forCellReuseIdentifier:@"SwapDetailTableViewCell"];

应该有效

答案 1 :(得分:1)

由于您要为细胞类型注册nib,因此需要将dequeueReusableCellWithIdentifier:替换为dequeueReusableCellWithIdentifier:forIndexPath:

实施例。变化:

ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier];

为:

ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier forIndexPath:indexPath];

你在viewDidLoad中有一些拼写错误。更新如下:

- (void)viewDidLoad {
    [super viewDidLoad];

    static NSString *ChatTableIdentifier = @"ChatTableViewCell";
    static NSString *ChatTableIdentifier2 = @"SwapDetailTableViewCell";

    UINib *nib = [UINib nibWithNibName: ChatTableIdentifier bundle:nil];  
    [self.tableView registerNib:nib forCellReuseIdentifier: ChatTableIdentifier];

    UINib *nib2 = [UINib nibWithNibName: ChatTableIdentifier2 bundle:nil];
    [self.tableView registerNib:nib2 forCellReuseIdentifier: ChatTableIdentifier2];
}