简体   繁体   中英

Detect touch in custom UITableViewCell

I have looked over the net but didn't find the answer for my situation. So I have a subclass of UITableCellView (I am try to do a cell quite similar to the cells in the Email app). I have added a label on the right side where I want to display a date using a blue color. Now, when I touch the cell, I want to change the color of the label, as the blue highlight will hide it. I have implemented thouchesBegan, Cancelled and Ended, but the problem is that there is some sort of "lag", the cell gets the blue highlight but the label changes its color after a few milliseconds. I am not sure how to change that.

Here is a snippet of my code:

#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

You could try calling the date.textColor=[UIColor whiteColor]; in the didSelectRowAtIndexpath method of the UITableViewController, which i think is called before your own implemented methods.

you can use UIGestures in this situation:

for using this mechanism you need to alter code and may be need to make some tweakings.

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.

also need to apply longTapGesture for the same. same thing working at my end.

Hope this will help you out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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