【问题标题】:Last button clickable in UITableViewCellUITableViewCell 中可点击的最后一个按钮
【发布时间】:2020-08-05 13:04:36
【问题描述】:

我在 UITableViewCell 中有多个按钮。只有最后一个按钮是可点击的。我在customCell.h 文件中定义了我的按钮

以下是我的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    CustomCell *cell;
    if (IS_IPAD) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"VCustomCell_iPad"];
    }else{
        cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell_iPhone"];
    }

    NSDictionary *dicCustom = [self.arrCustom objectAtIndex:indexPath.row];
    [cell configureVanStockDetail:dicCustom];

    [cell.btnOrder addTarget:self action:@selector(btnPurchaseOrderClicked:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

以下代码来自customCell.h文件

@property (strong, nonatomic) UIButton *btnOrder;

来自CustomCell.m 文件的代码

- (void) configureVanStockDetail:(NSDictionary *)objCustom {
   ...
   ...
    int count = 0
    for (NSDictionary *dicPO in arrPO) {
        self.btnOrder = [UIButton buttonWithType:UIButtonTypeCustom];
        self.btnOrder.translatesAutoresizingMaskIntoConstraints = NO;
        self.btnOrder.tag = count;
        [self.contentView addSubview:self.btnOrder];
        count++;
    }
}

从上面的代码中,谁能告诉我为什么只有最后创建的按钮是可点击的。

【问题讨论】:

  • 我创建了一个新项目并使用自定义 tableview 单元格制作了一个表格视图,并且选择器按钮被添加到所有单元格 github.com/CongL3/StackOverFlowQuestions 这让我相信它在代码之外的东西正在向我们展示
  • @CoNgL3 感谢您的努力。在这里,我在运行时以编程方式创建按钮。

标签: ios objective-c uitableview uibutton


【解决方案1】:

这是因为您只有一个按钮属性,并且您正在为要添加的每个新按钮覆盖该属性。

您可以在单元格中创建一个按钮数组,然后在 cellForRowAtIndexPath 中循环遍历该数组。

例如

CustomCell.h

添加

@property (strong, nonatomic) NSMutableArray *btnArray;

CustomCell.m file添加

[self.btnArray addObject:self.btnOrder];

表格视图代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ...
    ...

    for (UIButton *button in cell.btnOrder) {
        [button addTarget:self action:@selector(btnPurchaseOrderClicked:) forControlEvents:UIControlEventTouchUpInside];

    }
}

我认为这应该可行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多