我在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++;
}
}
通过以上代码,任何人都可以告诉我为什么只有最后创建的按钮才可单击。
这是因为您具有单个按钮属性,并且您要为要添加的每个新按钮覆盖该属性。
您可以在单元格中创建一个按钮数组,然后在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];
}
}
我认为这应该可行。