Iphone UITableView';s didSelectRowAtIndexPath仅每隔一次调用一次

Iphone UITableView';s didSelectRowAtIndexPath仅每隔一次调用一次,iphone,ios,objective-c,Iphone,Ios,Objective C,我正在使用自定义表格单元构建标准UITableView。我希望自定义单元格在TouchStart和touchEnd上执行某些操作。我在CustomCell类中实现了这些触摸方法,效果很好 这就是乐趣的开始。花了一段时间才弄清楚为什么自定义单元格所在的表没有接收到触摸。我想让单元格在触摸屏上做点什么,但同时让表格接收触摸屏,这样它就可以调用didSelectRow/didselectrowatindexpath。然后,我在自定义单元格类的所有touch方法中添加了[super-touchesbre

我正在使用自定义表格单元构建标准UITableView。我希望自定义单元格在TouchStart和touchEnd上执行某些操作。我在CustomCell类中实现了这些触摸方法,效果很好

这就是乐趣的开始。花了一段时间才弄清楚为什么自定义单元格所在的表没有接收到触摸。我想让单元格在触摸屏上做点什么,但同时让表格接收触摸屏,这样它就可以调用didSelectRow/didselectrowatindexpath。然后,我在自定义单元格类的所有touch方法中添加了[super-touchesbreated:touchs with-event: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];
}


}

不要覆盖
触摸开始:withEvent:
您应该让您的自定义单元格覆盖
setSelected:animated:
setHighlighted:animated:
在选择/突出显示时根据需要进行处理


UITableView
处理单元格的触摸,因为
UITableView单元格
具有复杂的视图层次结构。

而不是覆盖
触摸开始:使用事件:
您应该根据需要选择自定义单元格覆盖
设置:动画:
设置突出显示:动画:
当它们被选中/高亮显示时


UITableView
处理单元格的触摸,因为
UITableView单元格
具有复杂的视图层次结构。

与许多问题一样,问题不在于逻辑,而在于语法。我愚蠢地把我的实用工具命名为“tableView”。这也是DidSelectRowatineXpath中使用的参数UITable的名称。如果您注意到我在上面发布的代码中,对didSelectRow()的所有调用都调用self.tableView。相反,他们应该只调用tableView。虽然我不能完全解释为什么每隔一次调用didSelectRowAtIndexPath方法,但在更改此方法的参数名称后,一切都正常


总之,不要将属性命名为与参数名称相同的名称。

与许多问题一样,问题不在于逻辑,而在于语法。我愚蠢地把我的实用工具命名为“tableView”。这也是DidSelectRowatineXpath中使用的参数UITable的名称。如果您注意到我在上面发布的代码中,对didSelectRow()的所有调用都调用self.tableView。相反,他们应该只调用tableView。虽然我不能完全解释为什么每隔一次调用didSelectRowAtIndexPath方法,但在更改此方法的参数名称后,一切都正常

总之,不要将属性命名为与参数名称相同的名称