如何动态地将UIButton添加到UITableViewCell? [英] How to add UIButton to UITableViewCell dynamically?

查看:88
本文介绍了如何动态地将UIButton添加到UITableViewCell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动态地将UIButton添加到表格单元格中 - 请参阅以下要求。

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

上述方法强制tableView调整所选行的高度。但是当我在上面的方法中添加UIButton时,按钮不可见。

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.

我对[self.tableView reloadData]不感兴趣;

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

非常感谢任何帮助。

** * ** 已编辑 * ** * ***

*****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];
}


推荐答案

添加按钮将如果将其添加到 cellForRowAtIndexPath 中的单元格,则无法正常工作:。你需要做的是创建一个自定义的 UITableViewCell (请参阅这个问题的答案如何 - 在自定义UITableViewCell中更改自定义UIButton的位置

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.

cellForRowAtIndexPath :中,只需添加

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

这篇关于如何动态地将UIButton添加到UITableViewCell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆