Pages

Wednesday, January 18, 2012

How to implement single/double click on the UITableView?


  To get the touch/tap/move count on any control, there are certain methods in the UIResponder class.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event


To get the single/double click actions on UITableView, please follow the below steps:

1. Create a CustomTableViewCell, sub-class of the UITableViewCell. And override the above mentioned methods. 

#import ”CustomTableViewCell.h”
@implementation CustomTableViewCell
- (void)dealloc {
    [super dealloc];
}
- (void)drawRect:(CGRect)rect {
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // Use the same color and width as the default cell separator for now
    CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.5, 1.0);
    CGContextSetLineWidth(ctx, 0.5);
    for (int i = 0; i < [columns count]; i++) {
        CGFloat f = [((NSNumber*) [columns objectAtIndex:i]) floatValue];
        CGContextMoveToPoint(ctx, f, 0);
        CGContextAddLineToPoint(ctx, f, self.bounds.size.height);
    }
    CGContextStrokePath(ctx);
    [super drawRect:rect];
}
- (void)addColumn:(CGFloat)position {
    [columns addObject:[NSNumber numberWithFloat:position]];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *aTouch = [touches anyObject];
    NSLog(@”touchesBegan tapCount = %d”, [aTouch tapCount]);
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *aTouch = [touches anyObject];
    NSLog(@”touchesCancelled tapCount = %d”, [aTouch tapCount]);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *aTouch = [touches anyObject];
    NSLog(@”touchesEnded tapCount = %d”, [aTouch tapCount]);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *aTouch = [touches anyObject];
    NSLog(@”touchesMoved tapCount = %d”, [aTouch tapCount]);
}

2. In the UITableView’s cellForRowAtIndexPath method, add the 
CustomTableViewCell instead of UITableViewCell.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row];
    CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[[CustomTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
UILabel *regNoLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 3, 150, 15)];
//regNoLabel.text = [[self.resultArray objectAtIndex:indexPath.section] valueForKey:@”Ent_Regn_Nbr”];
regNoLabel.text = @”Apple”;
regNoLabel.font = [UIFont systemFontOfSize:12.0];
[cell addSubview:regNoLabel];
[regNoLabel release];
        return cell;


}
When you test the application with this, you can observe the tap count in the logs.

No comments:

Post a Comment