iphone - UITableView 的 didSelectRowAtIndexPath 仅每隔一段时间调用一次

标签 iphone ios objective-c

我正在构建一个带有自定义 tableCell 的标准 UITableView。我希望自定义单元格在 touchBegan 和 touchEnd 时做一些事情。我在自定义单元格类中实现了这些触摸方法,效果很好。

这就是乐趣的开始。花了一段时间才弄清楚为什么自定义单元格所在的表格没有收到触摸。我希望单元格在触摸时做一些事情,但也让表格接收触摸,以便它可以调用 didSelectRow/didDeselectRowAtIndexPath。然后,我将 [super touchesBegan:touches withEvent:event] 添加到自定义单元格类的所有触摸方法中,这使表格也可以接收单元格触摸……有点。当我运行程序时,前 2-3 次触摸注册到单元格和表格。之后,单元格继续每次都记录一次触摸,但表格每隔一次只会记录一次触摸。这种奇怪的行为让我很困惑。我研究了 View 层次结构概念以查看这是否可能是问题所在,并探索了诸如 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];
}


}

最佳答案

与其覆盖 touchesBegan:withEvent:,不如根据需要让自定义单元格覆盖 setSelected:animated:setHighlighted:animated:处理它们何时被选中/突出显示。

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

关于iphone - UITableView 的 didSelectRowAtIndexPath 仅每隔一段时间调用一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15508413/

相关文章:

ios - 混合方法 : Programmatically dismiss system dialogs like “<App> would like to access your contacts” in iOS

ios - 退出时保存当前状态,重新打开应用程序时自动重新加载

iphone - 矩形在 ZBar 扫描仪中带上相机?

ios - 设置iOS版本以提交App Store(无需Xcode !!!)

ios - SWRevealViewController 从 subview 推送

ios - 如何在SwiftUI分段选择器中更改选定的分段颜色

ios - 将横向模式下的模态视图显示在纵向模式下的另一个 View 之上

ios - 在iOS中的Facebook登录中检查是否已在此应用程序中注册

iphone - 从模态视图 Controller 更改选项卡栏应用程序的选定索引

iphone - 细胞标识符有什么用?