[英]UIButton subview of UITableViewCell unselecting 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
並調用performActionOnSelectionOfCellAtIndexPath
從內部didSelectCellAtIndexPath
與indexPath
也叫performActionOnSelectionOfCellAtIndexPath
從功能響應按下按鈕。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.