繁体   English   中英

在自定义UITableViewCell中检测触摸

[英]Detect touch in custom UITableViewCell

我已经看过网了,但是没有找到适合我的情况的答案。 所以我有一个UITableCellView的子类(我试图做一个与Email应用程序中的单元格非常相似的单元格)。 我在右侧添加了一个标签,该标签要使用蓝色显示日期。 现在,当我触摸单元格时,我想更改标签的颜色,因为蓝色高亮会隐藏它。 我已经实现了thouchesBegan,Cancel和Ended,但是问题是存在某种“滞后”,单元格显示蓝色高光,但是标签在几毫秒后更改了颜色。 我不确定该如何更改。

这是我的代码片段:

#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

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

您可以在这种情况下使用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];
}

自我= UITableViewCell。

同样也需要应用longTapGesture。 同样的事情在我的末端起作用。

希望这对您有所帮助。

暂无
暂无

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

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