OGeek|极客世界-中国程序员成长平台

标题: ios - 单击按钮时选择 tableViewCell [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 23:46
标题: ios - 单击按钮时选择 tableViewCell

我有一个带有自定义单元格的 tableView。 在每个单元格中我都有一个 UIButton。当我单击单元格内的按钮时,我希望该单元格突出显示,但是当我单击单元格时,我不希望这种情况发生。 你知道有什么办法吗? 谢谢

直到现在我有这个代码:

- (IBAction)buttonMethod: (id)sender {
UIButton *b = (UIButton *) sender;
UITableViewCell *cell = (UITableViewCell *)[[[sender superview] superview] superview];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
[cell setSelected:YES animated:YES];
}

但单元格也在 -didSelectRow 中突出显示。

- (UITableViewCell *)tableView: (UITableView *) tableView cellForRowAtIndexPathNSIndexPath *)indexPath {
static NSString *cellIdentifier = @"CustomCell";

CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
NSAssert(cell, @"cell -> nil");
NSDictionary *cellData = [_data objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;

[cell populateCellWithData:cellData];

return cell;
}



Best Answer-推荐答案


使用这个 UITableViewDelegate 方法:

- (NSIndexPath *)tableViewUITableView *)tableView willSelectRowAtIndexPathNSIndexPath *)indexPath {
    return nil;
}

这样,当用户点击单元格时无法选择它们,但您仍然可以通过编程方式选择它们。

也使用

selectRowAtIndexPath:animated:scrollPosition:

在您的 UITableView 上,而不是 UITableViewCell 上的 setSelected:animated: 可能会更好。单元格可以重复使用,当它发生时选择可能会消失。

关于ios - 单击按钮时选择 tableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24651103/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4