2011-04-07 69 views
1

在我的UITableView,我想在最後一個單元格中顯示加載指示器。 對於剩餘的單元格,我創建了一個工作正常的自定義表格視圖單元格。但是,當我滾動到結尾,它的崩潰,無法加載加載指示器(這是一個不同的自定義單元格視圖)。這是我的代碼。iPhone - UITableView顯示兩個不同的自定義單元格視圖

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    static NSString *CellIdentifier = @"HomeViewCell"; 

    UITableViewCell* cellToReturn = nil; 

    // Configure the cell. 
    NSInteger row = [indexPath row]; 
    if (row < [jokesTableArray count]) 
    { 
     CustomTableCell* cell = (CustomTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) 
     { 
      cell = [[[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     } 
     NSDictionary* dict = [jokesTableArray objectAtIndex:indexPath.row]; 
     NSNumber* rating = [[ServerCommunicator getInstance] getCurrentUserJokeRating:[dict objectForKey:@"id"]]; 
     cell.userRatedTheJoke = rating != nil ? YES : NO; 
     cell.titleLabel.text = [dict objectForKey:@"title"]; 
     cell.descriptionLabel.text = [dict objectForKey:@"text"]; 
     cell.upVotes.text = [NSString stringWithFormat:@"%2.2f",[[dict objectForKey:@"rating_count"] floatValue]]; 
     [cell setRating:rating != nil ? [rating intValue] : [[dict objectForKey:@"rating_count"] intValue]]; 
     NSString* authorName = [dict objectForKey:@"author"]; 
     if (authorName != nil && ![authorName isEqual:[NSNull null]]) 
     { 
      cell.author.text = [NSString stringWithFormat:@"By:%@",authorName];  
     } 
     cellToReturn = cell; 
    } 
    else 
    { 
     KMLoadingIndicatorCell* cell = (KMLoadingIndicatorCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) 
     { 
      KMLoadingIndicatorCell* cell = [[[KMLoadingIndicatorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
      cell.loadingLabel.text = @"Loading..."; 
      cellToReturn = cell; 
     } 
    } 

    return cellToReturn; 
} 

回答

0

您應該爲您的自定義單元格使用不同的單元標識符。

1

由於您使用的是相同的reuseIdentifier,行

KMLoadingIndicatorCell* cell = (KMLoadingIndicatorCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

是最有可能設置cell到現在被重用的CustomTableCell一個實例,而不是預期的KMLoadingIndicatorCell。您應該爲兩種不同類型的單元格使用不同的標識符。

相關問題