2014-11-05 29 views
1

在我tableViewcellForRowAtIndexPath方法,我使用的第一排和自定义单元格定期UITableViewCell对于其他行:的TableView崩溃的reloadData - “无法识别的选择发送到实例”

- (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]: unrecognized selector sent to instance

我不明白这是怎么回事..表加载完美的罚款(isLightTheme首先设置为true)当ViewController首次加载,但是当我改变isLightThemereloadData它崩溃。有人能帮我吗?谢谢。

回答

3

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

+1

谢谢,这工作:) – BadBoolean 2014-11-05 14:50:07

+0

很高兴帮助! UITableViews可能会令人沮丧,但你很快就会喜欢它们。 ;-) – 2014-11-05 14:55:55

相关问题