UITableViewCell 的 UIButton 子视图取消选择 UITableViewCell



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

有没有办法将触摸事件传递到其超级视图?还是一起存在不同的问题?

下面是 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并使用indexPathdidSelectCellAtIndexPath内部调用performActionOnSelectionOfCellAtIndexPath,并从响应按钮按下的函数调用performActionOnSelectionOfCellAtIndexPath

最新更新