0

I wanna add an UIButton to a table cell dynamically - please see the below requirements.

When user selects a cell, I'm simply adjusting the height of that table cell in

-(void) tableView:(UITableView*) tableView didSelectRowAtIndexPath:(UIIndexPath*) indexPath {
        selIndPath = [indexPath retain];
        [self.tableView beginUpdates];
        [self.tableView endUpdates];
}

The above method forces the tableView to adjust the height of that selected row. But when I added the UIButton in the above method, the button is not visible.

I'm not interested with the [self.tableView reloadData];

Any help greatly appreciated.

*****EDITED*******

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (selIndPath && selIndPath == indexPath) {
        //UITableViewCell* cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
//      UIButton* btn = (UIButton*)[[cell contentView] viewWithTag:1234];
//      btn.frame = CGRectMake(40, 60, 60, 24);
//      [cell setNeedsDisplay];
        return 90;
    }
    return 64;
}

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        [[cell contentView] setBackgroundColor:[UIColor clearColor]];
        [[cell backgroundView] setBackgroundColor:[UIColor clearColor]];
    }

    // THIS LINES OF CODE WORKS ONLY ON reloadData
    if (selIndPath && selIndPath == indexPath) {
        UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame = CGRectMake(40, 60, 60, 24);
        btn.tag = 1234;
        [btn setTitle:@"Hi" forState:UIControlStateNormal];
        [cell.contentView addSubview:btn];
    }

    // Configure the cell.
    cell.textLabel.text = @"Loading..";
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    selIndPath = [indexPath retain];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
    //[self.tableView reloadData];
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}
2
  • Can you show the code that you were using to add the UIButton?
    – Roy
    Dec 26, 2011 at 7:54
  • Thanks for the reply, I added some more extra code to my question. That might help you to understand it.
    – prathumca
    Dec 26, 2011 at 8:35

1 Answer 1

2

The addition of the button will not work if you add it to the cell in cellForRowAtIndexPath: . What you need to do is create a custom UITableViewCell( Refer to this question's answer on how- Changing the position of custom UIButton in custom UITableViewCell)

In the Custom cell, create a method

-(void) addButtonToCell
{
   UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(40, 60, 60, 24);
    btn.tag = 1234;
    [btn setTitle:@"Hi" forState:UIControlStateNormal];
    [self.contentView addSubview:btn];

}

Of course, how you add the button and what arguments you pass to this method is upto you, but you get the gist.

In cellForRowAtIndexPath: , simply add

if (selIndPath && selIndPath == indexPath) {
[cell addButtonToCell];
}
3
  • Thanks for the reply. This works if and only if you call [tableView reloadData]. As said earlier, I'm not interested with reloadData. Since my table has very large data and it voids the animation affect of row height changes.
    – prathumca
    Dec 26, 2011 at 9:25
  • @prathumca : how about using this method - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation on your table view. That will reload only the rows that you want to.
    – MadhavanRP
    Dec 26, 2011 at 9:57
  • I was giving a try that, but as of now no luck. The animation is not that much smooth and the buttons are not displayed/aligned correctly. Any help really appreciated.
    – prathumca
    Dec 26, 2011 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.