【问题标题】:Create button inside tableview cell without Interface Builder在没有界面生成器的表格视图单元内创建按钮
【发布时间】:2012-08-17 00:43:46
【问题描述】:

我尝试在不使用 Interface Builder 的情况下在 tableview 单元格内创建一个按钮。 我发现这段代码我认为它是用于创建按钮的:

- (void)viewDidLoad {
[super viewDidLoad];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(270,10,30,30);
[button setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];

}

但是当我尝试运行程序时,模拟器中没有任何显示。 我是否缺少使用此代码创建按钮的内容?

【问题讨论】:

    标签: iphone uitableview uibutton


    【解决方案1】:

    您需要将按钮添加到单元格 - 您尝试过吗

    [cell.contentView addSubview:button]
    

    ?

    调整按钮的框架以将其定位在 contentView 中并正确调整按钮的大小。

    【讨论】:

    • 感谢您的回答。我尝试了您建议的代码,但没有成功。
    • 应该的。您是否尝试过让一切变得非常简单,只在单元格中显示一个按钮,如下所示:stackoverflow.com/q/2633603/189804
    【解决方案2】:

    您应该在 cellforRow At index Datasource 方法中编写所有代码,并在单元格上添加您的按钮,您可以使用这行代码

    [cell.contentView addsubview:buttonobject];

    button.frame = CGRectMake(10,10,30,30);

    并且还可以根据您的构象更改游览按钮背景或样式,它是否存在。

    如果它的工作通知我们,它会很好地工作。

    欢迎您。

    【讨论】:

      【解决方案3】:

      您必须在此方法中添加代码,但首先将自定义单元格添加到您的表格视图中,

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      
       forwordBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
          [forwordBtn setTitle:@"Forward" forState:UIControlStateNormal];
      
          [forwordBtn addTarget:self 
                         action:@selector(forwordButtonPressed)
               forControlEvents:UIControlEventTouchUpInside];
          forwordBtn.frame = CGRectMake(0, 0, 10, 10);
      
          [cell.contentview addSubview:forwordBtn];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-27
        • 2021-10-06
        • 1970-01-01
        • 1970-01-01
        • 2021-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多