【问题标题】:How to add UIButton to UITableViewCell dynamically?如何动态地将 UIButton 添加到 UITableViewCell?
【发布时间】: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 的代码吗?
  • 感谢您的回复,我在我的问题中添加了一些额外的代码。这可能有助于您理解它。

标签: iphone ios ios4


【解决方案1】:

如果将按钮添加到cellForRowAtIndexPath: 中的单元格,则添加按钮将不起作用。你需要做的是创建一个自定义的UITableViewCell(请参考这个问题的答案-Changing the position of custom UIButton in custom UITableViewCell

在自定义单元格中,创建一个方法

-(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];

}

当然,如何添加按钮以及传递给此方法的参数取决于您自己,但您会明白要点。

cellForRowAtIndexPath: 中,只需添加

if (selIndPath && selIndPath == indexPath) {
[cell addButtonToCell];
}

【讨论】:

  • 感谢您的回复。当且仅当您调用 [tableView reloadData] 时,此方法才有效。如前所述,我对 reloadData 不感兴趣。由于我的表有非常大的数据,它使行高变化的动画效果无效。
  • @prathumca :如何在表格视图上使用此方法 - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation。这将只重新加载您想要的行。
  • 我正在尝试,但到目前为止还没有运气。动画不是那么流畅,按钮没有正确显示/对齐。任何帮助都非常感谢。
猜你喜欢
  • 1970-01-01
  • 2020-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多