nikkumang
nikkumang

Reputation: 1635

iPhone: Custom UITableViewCell with multiple areas that respond to taps?

I've been asked to create a custom UITableViewCell with multiple areas that can be tapped.

These areas won't have buttons or any graphics - they'll be invisible. 3 different methods will be called depending on which third of the cell the user taps i.e.

|| decrementFooCount || viewFooDetails || incrementFooCount ||

The cell has a few labels on it that need to be visible at all times - the fooName and fooCount.

I'm thinking perhaps three hidden UIButtons over the cell?

I also need to maintain the swipe to delete default behavior.

Upvotes: 3

Views: 1741

Answers (1)

Andrew
Andrew

Reputation: 2230

You can subclass your UITableViewCell and override the touchesBegan:withEvent: method. You can then get a CGPoint of where the touch was placed.

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
   UITouch* touch = touches.anyObject;
   CGPoint location = [touch locationInView:self];

   if (CGRectContainsPoint(myTestRect, location)) {
       // Touched inside myTestRect, do whatever...
   } else {
      // Let the default implementation take over.
      [super touchesBegan:touches withEvent:event];
   }
}

Andrew

Upvotes: 5

Related Questions