ios - TableView在reloadData上崩溃-“无法识别的选择器已发送到实例”

标签 ios objective-c iphone uitableview

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]:无法识别的选择器已发送到实例

我不明白这是怎么回事..第一次加载isLightTheme时,表加载得很好(首先将true设置为ViewController),但是当我更改isLightThemereloadData时,它崩溃了。有人可以帮我吗?谢谢。

最佳答案

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

关于ios - TableView在reloadData上崩溃-“无法识别的选择器已发送到实例”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26759853/

相关文章:

ios - 推送 View Controller 后出现意外延迟

iphone - 将资源目录层次结构包含到应用程序包中

iphone - 如何从 iOS 移动应用程序访问 ABAP Web Dynpro 组件

ios - 如何强制 uitableview 调整单元格的大小(在停用高度限制后)?

ios - UIPopoverPresentationController 动画 preferredContentSize

ios - tableView 单元格虚线边框未正确呈现

iphone - 当 UIImageView 离开 UIViewController 的框架时屏蔽它

ios - 条形码未生成

iOS - 手动创建给定大小的 UIButton

ios - 无法以任何新方法更改 socket 的属性