gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:22:06 26 4
gpt4 key购买 nike

我正在构建一个带有自定义 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com