Последняя кнопка, нажимаемая в UITableViewCell - PullRequest
0 голосов
/ 22 апреля 2020

У меня есть несколько кнопок в 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 file

- (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++;
    }
}

Исходя из приведенного выше кода, кто-нибудь может сказать мне, почему нажимается только последняя созданная кнопка.

1 Ответ

0 голосов
/ 23 апреля 2020

Это потому, что у вас есть одно свойство кнопки, и вы переопределяете свойство для каждой новой кнопки, которую вы добавляете.

Вы можете создать массив кнопок в ячейке, а затем l oop через этот массив внутри cellForRowAtIndexPath.

например

CustomCell.h

add

@property (strong, nonatomic) NSMutableArray *btnArray;

В CustomCell.m file add

[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];

    }
}

Я думаю, это должно работать.

...