Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add UIButton to UITableViewCell dynamically?
    primarykey
    data
    text
    <p>I wanna add an UIButton to a table cell dynamically - please see the below requirements.</p> <p>When user selects a cell, I'm simply adjusting the height of that table cell in </p> <pre><code>-(void) tableView:(UITableView*) tableView didSelectRowAtIndexPath:(UIIndexPath*) indexPath { selIndPath = [indexPath retain]; [self.tableView beginUpdates]; [self.tableView endUpdates]; } </code></pre> <p>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.</p> <p>I'm not interested with the [self.tableView reloadData];</p> <p>Any help greatly appreciated.</p> <p><em><strong></em>**<em>*</em>**</strong><em>EDITED</em><strong><em>*</em>**<em>*</em>***</strong></p> <pre><code>- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (selIndPath &amp;&amp; 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 &amp;&amp; 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]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload