UITableViewCell中的可单击的最后按钮

问题描述 投票:0回答:1

我在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++;
    }
}

通过以上代码,任何人都可以告诉我为什么只有最后创建的按钮才可单击。

ios objective-c uitableview uibutton
1个回答
0
投票

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

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

例如

CustomCell.h

添加

@property (strong, nonatomic) NSMutableArray *btnArray;

CustomCell.m file中添加

[self.btnArray addObject:self.btnOrder];

TableView代码

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

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

    }
}

我认为这应该可行。

© www.soinside.com 2019 - 2024. All rights reserved.