无法 select table 以编程方式查看单元格
Can't select table view cell programmatically
我有一个自定义 UITableViewCell
(MyCell),其中包含一个 UIButton
和另一个自定义单元格。我想以编程方式 select 其 UIButton
已被用户点击的单元格。实际上我可以在点击按钮时调用方法,但不能 select 单元格。我错过了什么?它不会崩溃,它会运行,但是这段代码除了 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");
}
你为什么不在单元格设置里面
-(void)awakeFromNib
{
//other code...
self.button.userInteractionEnabled = NO;
}
tap 将通过响应链传递到 cell。
好吧,假设您确定 cell.acceptLabel
是一个 UIButton(按钮的奇怪名称)并且 if 语句在您的情况下为真:那么剩下的就是说您的第一个版本应该是对的,只是你用错了indexPath。您确实将单元格的 indexPath.row 指定为按钮的标签(非常好)。但是那你为什么不使用它呢?
- (void)didTapBttn:(id)sender {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:((UIButton *)sender).tag inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
NSLog(@"button tapped");
}
试试这个:)
PS:不过,您仍然应该重新考虑您的方法背后的意义。选择一个表格视图单元格通常是通过点击一个单元格来完成的。
我有一个自定义 UITableViewCell
(MyCell),其中包含一个 UIButton
和另一个自定义单元格。我想以编程方式 select 其 UIButton
已被用户点击的单元格。实际上我可以在点击按钮时调用方法,但不能 select 单元格。我错过了什么?它不会崩溃,它会运行,但是这段代码除了 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");
}
你为什么不在单元格设置里面
-(void)awakeFromNib
{
//other code...
self.button.userInteractionEnabled = NO;
}
tap 将通过响应链传递到 cell。
好吧,假设您确定 cell.acceptLabel
是一个 UIButton(按钮的奇怪名称)并且 if 语句在您的情况下为真:那么剩下的就是说您的第一个版本应该是对的,只是你用错了indexPath。您确实将单元格的 indexPath.row 指定为按钮的标签(非常好)。但是那你为什么不使用它呢?
- (void)didTapBttn:(id)sender {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:((UIButton *)sender).tag inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
NSLog(@"button tapped");
}
试试这个:)
PS:不过,您仍然应该重新考虑您的方法背后的意义。选择一个表格视图单元格通常是通过点击一个单元格来完成的。