У меня есть пользовательский UITableViewCell
(MyCell), который содержит UIButton
и другую пользовательскую ячейку. Я хотел бы выбрать ячейку программно, чей UIButton
был использован пользователем. На самом деле я могу вызывать методы при нажатии кнопок, но не могу выбрать ячейку. Что я пропустил? Он не сбой, он работает, но этот код не вносит никаких изменений, кроме NSLog.Невозможно выбрать программную ячейку таблицы
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([object[@"num"] isEqualToString:@"one"]) {
MyCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
//... cell display
cell.acceptLabel.tag = indexPath.row;
[cell.acceptLabel addTarget:self action:@selector(didTapBttn:) forControlEvents:UIControlEventTouchUpInside];
return cell;
} else {
MyCustomTableViewCellB *cellMessage = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
//... cell display
return cellMessage;
}
}
//1. version
- (void)didTapBttn:(id)sender {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:
UITableViewScrollPositionNone];
NSLog(@"button tapped");
}
// 2. version
- (void)didTapBttn:(id)sender {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView.delegate tableView:self.tableView didSelectRowAtIndexPath:indexPath];
NSLog(@"button tapped");
}
Что вы пытаетесь достичь этим? Доступ к 'didSelectRowAtIndexPath' или установка фона ячейки как выделенного? – random
@ случайный доступ к 'didSelectRowAtIndexPath'. – rihe
Просто переместите содержимое с 'didSelectRowAtIndexPath' на другой метод и укажите его. – random