Chris
Chris

Reputation: 11

Add subview when UITableView row is selected?

I'm working on an iPhone app that parses an XML file into a UITableView. It lists all of the titles of items within the XML file, and I'm trying to have the cell expand and add the body content of the selected item when a specified cell is selected.

I can get the cell to expand correctly, but when I have had no luck adding the body content as a subview. When populating the cell at cellForRowAtIndexPath I can add the body content and it displays fine.

My question is, how do I add a subview to the selected cell after it has been selected?

My cellForRowAtIndexPath function, with the 'functioning' bodyLabel commented out:

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

static NSString *cellID = @"Cell";
issue *curIssue = [[parser issues] objectAtIndex:indexPath.row];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];


    CGRect nameFrame = CGRectMake(0,2,300,15);
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:nameFrame];
    nameLabel.tag = 1;
    nameLabel.font = [UIFont boldSystemFontOfSize:12];
    [cell.contentView addSubview:nameLabel];

    CGRect bodyFrame = CGRectMake(0,16,300,60);

    UILabel *bodyLabel = [[UILabel alloc] initWithFrame:bodyFrame];
    bodyLabel.tag = 2;
    bodyLabel.numberOfLines = 10;
    bodyLabel.font = [UIFont systemFontOfSize:10];
    bodyLabel.hidden = YES;

    [cell.contentView addSubview:bodyLabel];

}



    UILabel *nameLabel = (UILabel *)[cell.contentView viewWithTag:1];
    nameLabel.text     = [curIssue name];

    UILabel *bodyLabel = (UILabel *)[cell.contentView viewWithTag:2];
    bodyLabel.text     = [curIssue body];

    return cell;
}

Here is my heightForRowAtIndexPath function, where I'm trying to add the subview. When trying to execute, I receive a EXC_BAD_ACESS exception when trying to alloc the *bodyLabel element.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger defCellHeight  = 15;
NSInteger heightModifier = 10;
if([self cellIsSelected:indexPath]){
    return defCellHeight * heightModifier;
}

return defCellHeight;
}

My didSelectRowAtIndexPath function, which allows the cell to grow/shrink:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{     
    BOOL isSeld = ![self cellIsSelected:indexPath];

    NSNumber *seldIndex = [NSNumber numberWithBool:isSeld];
    [selectedIndexes setObject:seldIndex forKey:indexPath];

    UILabel *label = (UILabel *)[tableView viewWithTag:2];
    label.hidden = NO;

    [curIssView beginUpdates];
    [curIssView endUpdates];
}

And finally, the cellIsSelected helper function which returns true if the cell is selected:

-(bool) cellIsSelected:(NSIndexPath *)indexPath
{
    NSNumber *selIndex = [selectedIndexes objectForKey:indexPath];
    return selIndex == nil ? FALSE : [selIndex boolValue];
}

You can find the full source file here.

Upvotes: 1

Views: 2071

Answers (3)

Chris C
Chris C

Reputation: 3251

To add a subview to the cell when you select it, it's pretty straightforward:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{     
    /*
     .. your other setup
    */

    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell.contentView addSubview:<your subview>];
}

Upvotes: 0

cherbear
cherbear

Reputation: 253

Seems like you're allocating and adding the same UILabel's multiple times. You should only have to do it once within the cellForRowAtIndexPath method. You might also want to set the bodyLabel to hidden in the cellForRowAtIndexPath method, then set it to not hidden when the cell has been selected. With something like:

bodyLabel.hidden = YES;

Another thing. Why are you deselecting the row within the didSelectRowAtIndexPath?

Upvotes: 1

Stephen Heaps
Stephen Heaps

Reputation: 116

Why are you creating a UILabel in heightForRowAtIndexPath? If you're trying to add it to the row, use the cellForRowAtIndexPath method you used above.

Simply adding a subview to the UITableViewCell or anywhere else in view with UIView's addSubview method inside of didSelectRowAtIndexPath will work for displaying a subview when a UITableView row is selected.

Upvotes: 0

Related Questions