Iphone 通过自定义UITableViewCell和UITableView处理多个对象的触摸事件
我正在处理一个UITableView,它将包含两种不同类型的自定义UITableViewCells,每个单元格都设置为UITable的不同部分。我希望单元格在触摸时高亮显示为不同的颜色,然后在触摸结束后恢复为以前的颜色。由于这不遵循标准UITableViewCell的标准触摸高光实现,因此我在自定义单元格类中添加了touchesBegind()和TouchesEnd()方法以获得此效果。但是,UITable不会捕捉任何接触,因此不会执行其他标准表功能所需的didSelectRowatineXpath()或declerowatineXpath()方法。如果我注释掉自定义单元格的touchsbegind/touchsend方法,那么UITable将捕获这些触摸并调用这些方法。是否有一种方法可以让自定义单元格执行其触摸方法,以及UIT能够在单次触摸时执行didSelect/DISELECT方法?这是我在UITable上的第一次尝试,并且使用了预期的自定义功能,我为此付出了很大的努力。如果我的代码中除了我的问题之外还有什么不合适的地方,我深表歉意,并提前感谢您的建议。以下是这些部分的代码: 自定义单元格的方法:Iphone 通过自定义UITableViewCell和UITableView处理多个对象的触摸事件,iphone,ios,objective-c,ipad,Iphone,Ios,Objective C,Ipad,我正在处理一个UITableView,它将包含两种不同类型的自定义UITableViewCells,每个单元格都设置为UITable的不同部分。我希望单元格在触摸时高亮显示为不同的颜色,然后在触摸结束后恢复为以前的颜色。由于这不遵循标准UITableViewCell的标准触摸高光实现,因此我在自定义单元格类中添加了touchesBegind()和TouchesEnd()方法以获得此效果。但是,UITable不会捕捉任何接触,因此不会执行其他标准表功能所需的didSelectRowatineXpa
-(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) {
self.currentlySelected = YES;
self.accessoryButton.hidden = NO;
}
else {
self.currentlySelected = NO;
self.accessoryButton.hidden = YES;
}
}
}
-(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];
}
}
对于UITableViewController中的didSelect/DeselectRow方法:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.tableView.bounds.size.width, 30.0)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
CGFloat isRed = 84.0/255.0;
CGFloat isGreen = 84.0/255.0;
CGFloat isBlue = 84.0/255.0;
self.headerColor = [[UIColor alloc]initWithRed:isRed green:isGreen blue:isBlue alpha:0.5];
headerLabel.backgroundColor = self.headerColor;
headerLabel.opaque = NO;
headerLabel.textColor = self.textColor;
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont systemFontOfSize:18.0];
headerLabel.frame = CGRectMake(0.0, 0.0, self.tableView.bounds.size.width, 30.0);
if (section == 0) {
[headerView setBackgroundColor:self.backgroundColor];
headerLabel.text = @"Alley";
[headerView addSubview:headerLabel];
}
else {
[headerView setBackgroundColor:self.backgroundColor];
headerLabel.text = @"Bowlr";
[headerView addSubview:headerLabel];
}
return headerView;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(self.lastIndexPathForAlleyCells == indexPath) {
[self.tableView beginUpdates];
[self.tableView deselectRowAtIndexPath:self.lastIndexPathForAlleyCells animated:NO];
[self.tableView endUpdates];
}
if(indexPath.section == 0) {
[self.tableView beginUpdates];
[self.tableView deselectRowAtIndexPath:self.lastIndexPathForAlleyCells animated:NO];
AlleyTableViewCell *cell = (AlleyTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
cell.accessoryButton.hidden = NO;
self.lastIndexPathForAlleyCells = indexPath;
[self.tableView endUpdates];
}
else if(indexPath.section == 1) {
PlayerTableViewCell *cell = (PlayerTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
[cell.accessoryButton setImage:[UIImage imageNamed:@"Checkmark.png"] forState:UIControlStateNormal];
[cell setSelected:NO animated:NO];
}
}
使用UIgestureRecognitor而不是
触摸代理
或
在
didSelectRowAtIndexPath
上获取相应的单元格,然后执行所需操作。如果希望单元格的选择颜色不同于默认的蓝色,只需在cellForRowAtIndexPath方法中设置单元格的backgroundView属性:
UIView *backgroundView = [[UIView alloc] initWithFrame:cell.frame];
backgroundView.backgroundColor = [UIColor colorWithRed:0.90f green:0.90f blue:0.90f alpha:1.00f];
cell.selectedBackgroundView = backgroundView;
这个问题的答案实际上是一个普通问题的普通答案。考虑到将单元格定制为UITable,如何实现这一点可能还不太清楚 它必须处理自定义单元格中的触摸事件,然后将触摸事件发送到父视图(在本例中为UITable) 以下是帮助解决此问题的帖子链接: 覆盖子类中的touch方法时,写出希望看到子类执行的功能,并在每个方法的末尾包括: [超级触摸开始:触摸事件:事件] 对每个触摸方法执行完全相同的操作:触摸移动、触摸结束、触摸取消 My UITable现在接收触摸事件,didSelectRowAtIndexPath和DescelRowatindexpath方法现在可以工作,因为它们接收触摸事件。我希望这对其他人有帮助
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan: touches withEvent: event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved: touches withEvent: event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded: touches withEvent: event];
}
我考虑过这条路线,并在到达这一点之前尝试过。但是,所需的功能不是在选择时突出显示单元格,而是在初次触摸时突出显示单元格。此路由仅在将单元格设置为选中后高亮显示。如果我使用UIGestureRecognitor而不是触摸代理,当单元格使用GestureRecognitor时,UITable是否能够拾取触摸?我将如何实现一个UIGestureRecognitor,它将在触摸时高亮显示,并在触摸结束后恢复到以前的颜色?非常感谢。