the_critic
the_critic

Reputation: 12820

Add UIView to reused UITableViewCell?

I am desperately trying to achieve the following effect: I have got a tableview where I would like to expand the cells on selection and show some additional information on it. I reuse table cells, just FYI. In the past I was able to achieve such an effect in another application, but this just does not seem to work for me at all. All the methods that are vital for this to happen are called and the row expands beautifully, but the additionalInfo table in the showExpandedContents method just will not appear!

So here is some code for you:

Here is the cell setup:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellID = @"MyCustomCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if(cell == nil){
        cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }

    ((MyCustomCell*)cell).data = [arrayToDisplay objectAtIndex:indexPath.row];


    return cell;
}

Here is the selection method:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    //setting the current cell as the expandedRow, so that I can collapse it later
    expandedRow = [tableView cellForRowAtIndexPath:indexPath];
    [((MyCustomCell*)expandedRow) showExpandedContents];
    [tableView beginUpdates];
    [self changeRowHeightAtIndex:indexPath.row height:330];
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [tableView endUpdates];



}

Here is the -showExpandedContents method in MyCustomCell where I would like to display the additional information:

-(void)showExpandedContents{

    additionalInfo = [[UITableView alloc] initWithFrame:CGRectMake(0, 80, self.frame.size.width, 250)];
    [additionalInfo loadContent];
    additionalInfo.backgroundColor = UIColorFromRGB(0xf9f9f9, 1.0);
    additionalInfo.layer.zPosition = MAXFLOAT;
    [self.contentView addSubview:additionalInfo];

}

Upvotes: 2

Views: 755

Answers (1)

William Entriken
William Entriken

Reputation: 39263

Looks like your

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

is giving you a fresh cell, different than the one that had showExpandedContents called.

Removing this line should fix the problem.

Upvotes: 1

Related Questions