Reputation: 410
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];
}
Upvotes: 0
Views: 7040
Reputation: 2830
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];
}
Upvotes: 2