【问题标题】:TableView Crashes on reloadData - 'unrecognized selector sent to instance'TableView 在 reloadData 上崩溃 - '发送到实例的无法识别的选择器'
【发布时间】:2014-11-05 14:40:21
【问题描述】:

在我的tableViewcellForRowAtIndexPath 方法中,我对第一行使用自定义单元格,对其他行使用常规UITableViewCell

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

    static NSString *MyIdentifier = @"Cell";

    if (indexPath.section == 0){
        CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }

        cell.titleLabel.text = @"Custom cell";

        if (isLightTheme){
           cell.titleLabel.textColor = [UIColor blackColor];
        }
        else{
           cell.titleLabel.textColor = [UIColor whiteColor];
        }

        return cell;
    }
    else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:MyIdentifier];
        }

        cell.textLabel.text = @"Other cells";

        if (isLightTheme){
            cell.textLabel.textColor = [UIColor blackColor];
        }
        else{
            cell.textLabel.textColor = [UIColor whiteColor];
        }

        return cell;

    }

}

然后我调用一个方法来改变 isLightTheme 并重新加载数据:

-(void)changeTheme{

   isLightTheme = false;
   [self.myTable reloadData];
}

...但我的应用程序在此行崩溃:

    cell.titleLabel.text = @"Custom cell";

出现错误:

'-[UITableViewCell titleLabel]: 无法识别的选择器发送到实例

我不明白发生了什么.. 当ViewController 第一次加载时,表格加载得非常好(isLightTheme 首先设置为true),但是当我更改isLightThemereloadData 时崩溃。有人可以帮帮我吗?谢谢。

【问题讨论】:

    标签: ios objective-c iphone uitableview


    【解决方案1】:

    两个单元格的重用标识符相同。自定义和默认。为每个使用唯一值。

    【讨论】:

    • 很高兴为您提供帮助! UITableViews 可能令人沮丧,但你很快就会爱上它们。 ;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多