Satyam
Satyam

Reputation: 15894

iPhone - UITableView to show two different custom cell views

In my UITableView, I want to show loading indicator in the last cell. For remaining cells, I created a custom table view cell which is working fine. But when I scroll to the end, its crashing and not able to load loading indictor (which is a different custom cell view). Here's my code.

- (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;
}

Upvotes: 1

Views: 2103

Answers (2)

Anomie
Anomie

Reputation: 94794

Since you are using the same reuseIdentifier, the line

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

is most likely setting cell to an instance of CustomTableCell that is now being reused, rather than the expected KMLoadingIndicatorCell. You should be using different identifiers for the two different types of cells.

Upvotes: 1

drewag
drewag

Reputation: 94683

You should use a different Cell Identifier for your custom cell.

Upvotes: 0

Related Questions