【问题标题】:Detect touch in custom UITableViewCell在自定义 UITableViewCell 中检测触摸
【发布时间】:2012-05-21 11:52:34
【问题描述】:

我查看了网络,但没有找到适合我情况的答案。所以我有一个 UITableCellView 的子类(我尝试做一个与电子邮件应用程序中的单元格非常相似的单元格)。我在右侧添加了一个标签,我想在其中使用蓝色显示日期。现在,当我触摸单元格时,我想更改标签的颜色,因为蓝色突出显示会隐藏它。我已经实现了 thouchesBegan、Canceled 和 Ended,但问题是存在某种“滞后”,单元格获得蓝色突出显示,但标签在几毫秒后改变其颜色。我不知道如何改变它。

这是我的代码的 sn-p:

#import "AnnouncementCell.h"

@implementation AnnouncementCell
@synthesize date;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
    // Initialization code
    date=[[UILabel alloc] initWithFrame:CGRectMake(220, 13, 100, 22)];
    date.textColor=[UIColor blueColor];
    date.font=[UIFont fontWithName:@"Helvetica" size:14.0];
    [self addSubview:date];
}
return self;
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
date.textColor=[UIColor whiteColor];
[super touchesBegan:touches withEvent:event];

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
date.textColor=[UIColor blueColor];    
[super touchesEnded:touches withEvent:event];

}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
date.textColor=[UIColor blueColor];   
[super touchesCancelled:touches withEvent:event];

}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

@end

【问题讨论】:

    标签: iphone uitableview detect touchesbegan


    【解决方案1】:

    您可以尝试在 UITableViewController 的 didSelectRowAtIndexpath 方法中调用 date.textColor=[UIColor whiteColor];,我认为该方法在您自己实现的方法之前调用。

    【讨论】:

    • 不,它不起作用,didSelectRowAtIndexpath 在你抬起手指后被调用。
    【解决方案2】:

    您可以在这种情况下使用 UIGestures:

    为了使用这种机制,您需要更改代码并且可能需要进行一些调整。

    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]
     initWithTarget:self 
     action:@selector(tapDetected:)];
    doubleTap.numberOfTapsRequired = 1;
    [self addGestureRecognizer:doubleTap];
    [doubleTap release];
    
    -(void)tapDetected:(UIGestureRecognizer*) gesture {
    self.lblTitle.backgroundColor=[UIColor blueColor];
    [self setNeedsDisplay];
    }
    

    self = UITableViewCell.

    同样需要使用 longTapGesture。 同样的事情在我的最后工作。

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多