我正在使用自定义 tableCells 构建标准 UITableView。我希望自定义单元格在 touchBegan 和 touchEnd 时执行某些操作。我在自定义单元类中实现了这些触摸方法,它工作得很好。
这就是乐趣开始的地方。花了一段时间才弄清楚为什么自定义单元格所在的表格没有收到触摸。我希望单元格在触摸时执行某些操作,但也让表格接收该触摸,以便它可以调用 didSelectRow/didDeselectRowAtIndexPath。然后我将 [super touchesBegan:touches withEvent:event] 添加到自定义单元格类的所有触摸方法中,这使表格也可以接收单元格触摸......有点。当我运行程序时,前 2-3 次触摸记录到单元格和表格中。之后,单元格继续每次注册一次触摸,但表格只会每隔一次注册一次触摸这种奇怪的行为让我很难过。我研究了视图层次结构的概念,看看这是否可能是问题,以及诸如 hitTest 之类的替代方案,但没有什么是明显的解决方案。有没有人有任何理论来解释为什么会发生这种情况?这是自定义单元格触摸和 didSelectRowAtIndexPath 方法的代码。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches]anyObject];
if([touch view] == self.nameLabel) {
CGFloat isRed = 151.0/255.0;
CGFloat isGreen = 151.0/255.0;
CGFloat isBlue = 151.0/255.0;
self.nameLabel.backgroundColor = [[UIColor alloc]initWithRed:isRed green:isGreen blue:isBlue alpha:1.0];
if(!self.currentlySelected) {
NSLog(@"if(!self.currentlySelected");
self.currentlySelected = YES;
[self.accessoryButton setImage:[UIImage imageNamed:@"Checkmark.png"]forState:UIControlStateNormal];
self.accessoryButton.hidden = NO;
}
else {
NSLog(@"if(self.currentlySelected");
self.currentlySelected = NO;
self.accessoryButton.hidden = YES;
[self.accessoryButton setImage:nil forState:UIControlStateNormal];
}
}
NSLog(@"Touch Began");
[super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches]anyObject];
if([touch view] == self.nameLabel) {
self.nameLabel.backgroundColor = [[UIColor alloc]initWithRed:self.isRed green:self.isGreen blue:self.isBlue alpha:1.0];
}
NSLog(@"Touch End");
[super touchesEnded: touches withEvent: event];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesCancelled:touches withEvent:event];
}
didSelectRowAtIndexPath:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Method didselectRow was Called");
//If it is in the Alley section
if(indexPath.section == 0) {
//If the selected cell is the same as the last selected cell
if([self.lastIndexPathForAlleyCells isEqual:indexPath]) {
NSLog(@"Same");
[self.tableView beginUpdates];
AlleyTableViewCell *previousCell = (AlleyTableViewCell *)[self.tableView cellForRowAtIndexPath:self.lastIndexPathForAlleyCells];
previousCell.currentlySelected = NO;
self.lastIndexPathForAlleyCells = nil;
previousCell.accessoryButton.hidden = YES;
[self.tableView endUpdates];
}
//Else the selected cell is not the last selected cell
else {
[self.tableView beginUpdates];
NSLog(@"Not the same");
AlleyTableViewCell *previousCell = (AlleyTableViewCell *)[self.tableView cellForRowAtIndexPath:self.lastIndexPathForAlleyCells];
previousCell.currentlySelected = NO;
previousCell.accessoryButton.hidden = YES;
AlleyTableViewCell *cell = (AlleyTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
cell.currentlySelected = YES;
cell.accessoryButton.hidden = NO;
self.lastIndexPathForAlleyCells = indexPath;
[self.tableView endUpdates];
}
}
else if(indexPath.section == 1) {
NSLog(@"Section 1 shouldn't be here");
PlayerTableViewCell *cell = (PlayerTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
[cell.accessoryButton setImage:[UIImage imageNamed:@"Checkmark.png"] forState:UIControlStateNormal];
[cell setSelected:NO animated:NO];
}
}