【发布时间】:2011-12-26 17:06:29
【问题描述】:
我想动态地将 UIButton 添加到表格单元格 - 请参阅以下要求。
当用户选择一个单元格时,我只是在
中调整该表格单元格的高度-(void) tableView:(UITableView*) tableView didSelectRowAtIndexPath:(UIIndexPath*) indexPath {
selIndPath = [indexPath retain];
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
上述方法强制 tableView 调整所选行的高度。但是当我在上面的方法中添加 UIButton 时,按钮是不可见的。
我对 [self.tableView reloadData] 不感兴趣;
非常感谢任何帮助。
*****已编辑*** ****
- (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];
}
【问题讨论】:
-
你能展示你用来添加 UIButton 的代码吗?
-
感谢您的回复,我在我的问题中添加了一些额外的代码。这可能有助于您理解它。