gpt4 book ai didi

ios - UITableViewCell 的 UIButton subview 取消选择 UITableViewCell?

转载 作者:行者123 更新时间:2023-11-29 03:23:11 28 4
gpt4 key购买 nike

我在 UITableViewCell 中有一个 subview ,其中包含一个 UILabel 和一个 UIButton。当我按下按钮时,它出于某种原因取消选择 UITableViewCell。

有没有办法将触摸事件传递给它的 super View ?还是存在不同的问题?

这里是 CellForRowAtIndexPath 的代码。我计划创建一个 UITableViewCell 的自定义子类,这样我就可以正确处理单元格重用,所以觉得没有必要提及这一点。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CountModel *cellModel = self.viewModel.data[indexPath.row];

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RootViewControllerMetricCell"];

cell.textLabel.text = cellModel.title;

// Custom background color for selection
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = UIColorFromRGB(0xF3F3F3);
bgColorView.layer.masksToBounds = YES;
[cell setSelectedBackgroundView:bgColorView];

// Construct subview and append to bottom (only visible when selected)
UIView *cellSubView = [[UIView alloc] initWithFrame:CGRectMake(0, 125, cell.frame.size.width, 25)];

// UILabel for display of current count
UILabel *lblCount = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 25, 25)];
lblCount.text = [NSString stringWithFormat:@"%d", [cellModel.count intValue]];

// Create UIButton for incrementing
UIButton *incrementButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
incrementButton.frame = CGRectMake(45, 0, 25, 25);
[incrementButton setTitle:@"+" forState:UIControlStateNormal];

// Enable touch event for button view
[incrementButton addTarget:self action:@selector(countButtonTouchDown:) forControlEvents:UIControlEventTouchDown];

[cellSubView addSubview:lblCount];
[cellSubView addSubview:incrementButton];

[cell addSubview:cellSubView];

return cell;
}

最佳答案

如何在函数中添加 [self.tableView cellForRowAtIndePath:] setSelected:] 以响应按下按钮以恢复相同的选中/取消选中状态。

更新:

didSelectCellAtIndexPath 中的代码转移到另一个函数,比如 performActionOnSelectionOfCellAtIndexPath 并从 didSelectCellAtIndexPath 中调用 performActionOnSelectionOfCellAtIndexPath使用 indexPath 并从响应按钮按下的函数中调用 performActionOnSelectionOfCellAtIndexPath

关于ios - UITableViewCell 的 UIButton subview 取消选择 UITableViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20816374/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com