简体   繁体   English

如何动态地将UIButton添加到UITableViewCell?

[英]How to add UIButton to UITableViewCell dynamically?

I wanna add an UIButton to a table cell dynamically - please see the below requirements. 我想动态地将UIButton添加到表格单元格中 - 请参阅以下要求。

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. 上面的方法强制tableView调整所选行的高度。 But when I added the UIButton in the above method, the button is not visible. 但是当我在上面的方法中添加UIButton时,按钮不可见。

I'm not interested with the [self.tableView reloadData]; 我对[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];
}

The addition of the button will not work if you add it to the cell in cellForRowAtIndexPath : . 如果将其添加到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 ) 你需要做的是创建一个自定义的UITableViewCell (请参阅这个问题的答案 - 如何在自定义UITableViewCell中更改自定义UIButton的位置

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 cellForRowAtIndexPath :中,只需添加

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM