2014-11-05 26 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

相關問題