UITutView子视图的UITableViewCell取消选择UITableViewCell?

时间:2013-12-28 15:52:39

标签: ios objective-c uitableview uibutton

我在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;
}

1 个答案:

答案 0 :(得分:1)

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

更新:

didSelectCellAtIndexPath内的代码转移到另一个函数,说performActionOnSelectionOfCellAtIndexPath并使用performActionOnSelectionOfCellAtIndexPathdidSelectCellAtIndexPath内部调用indexPath,然后调用{{ 1}}从响应按钮的功能按下。