我正在使用自定义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];
}
}
答案 0 :(得分:2)
您应该根据需要覆盖touchesBegan:withEvent:
和setSelected:animated:
,而不是覆盖setHighlighted:animated:
,以便在选择/突出显示它们时进行处理。
让UITableView
处理您的单元格的触摸,因为UITableViewCell
具有复杂的视图层次结构。
答案 1 :(得分:1)
与许多问题一样,问题不在于逻辑,而在于语法。我愚蠢地命名了我的UITable“tableView”。这也恰好是didSelectRowAtIndexPath中使用的参数UITable的名称。如果你注意到我上面发布的代码,所有对didSelectRow()的调用都会调用self.tableView。相反,他们应该只是调用tableView。虽然我无法完全解释为什么每隔一次调用didSelectRowAtIndexPath方法,但在更改此方法的参数名称后,一切正常!
总之,不要将您的属性命名为与参数名称相同的名称。