繁体   English   中英

通过自定义UITableViewCell和UITableView处理对多个对象的触摸事件

[英]Handling Touch Events to Multiple Objects via Custom UITableViewCell and UITableView

我正在处理一个UITableView,它将包含两种不同类型的自定义UITableViewCells,每个单元格设置为UITable的不同部分。 我希望单元格在触摸时突出显示为其他颜色,然后在触摸结束后恢复为以前的颜色。 由于这没有遵循标准UITableViewCell的标准触摸突出显示实现,因此我向自定义单元格类添加了TouchesBegan()和TouchesEnd()方法以获得此效果。 但是,UITable不会捕获任何接触,因此不会执行我为其他标准表功能所需的didSelectRowAtIndexPath()或deselectRowAtIndexPath()方法。 如果我注释掉自定义单元格的TouchesBegan / TouchesEnd方法,则UITable将捕获触摸并调用这些方法。 有没有一种方法可以让自定义单元格执行其触摸方法,以及让UITable在一次触摸时执行didSelect / deselect方法? 这是我第一次在UITable上进行破解,并具有预期的自定义功能,我为此付出了很多努力。 抱歉,代码中除了我的问题以外的任何地方都不合适,并在此先感谢您的建议。 这是下面这些部分的代码:

自定义单元格的触摸方法:

-(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];
}
}

使用UIGestureRecognizer代替touch delegates

要么

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方法时,请写出您希望看到子类执行的功能,每个方法的末尾包括:

[super touchesBegan:touches withEvent:event];

对每种触摸方法执行完全相同的操作:touchesMoved,touchesEnd,touchesCancelled。

我的UITable现在接收触摸事件,并且didSelectRowAtIndexPath和deselectRowAtIndexPath方法现在可以工作,因为它们接收触摸事件。 我希望这可以帮助其他人!

- (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];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM