2011-04-07 43 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。您应该为两种不同类型的单元格使用不同的标识符。

相关问题