Bama91
Bama91

Reputation: 904

How to handle touch event on UILabel as subview of UITableViewCell?

My app has a custom UITableView. In the cellForRowAtIndexPath delegate method of its UIViewController I am instantiating custom UITableViewCell objects that contain multiple custom UILabels (actually a sub-class of OHAttributedLabel) as subviews of the content view.

I have tried setting userInteractionEnabled = YES on the label, then adding touch events in the view controller, but that isn't working.

Ideas?

Thanks

Upvotes: 8

Views: 14928

Answers (6)

kingboylee0595
kingboylee0595

Reputation: 1

you can use TTTAttributedLabel to instead it. it's very easy. when you initial the UITableViewCell,you can delegate:TTTAttributedLabelDelegate like :

@interface MyTableViewCell : UITableViewCell<TTTAttributedLabelDelegate>{
    UILabel *nameLabel;
    TTTAttributedLabel *valueLabel;
}

when you initial ,you could add link to label :

 [valueLabel addLinkToPhoneNumber:valueStr withRange:NSMakeRange(0, valueStr.length)];

so,you could do anything you want:

- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithPhoneNumber:(NSString *)phoneNumber{
   //do anything you want.

}

Upvotes: 0

user2400553
user2400553

Reputation: 1

I just had the problem with using static table cells for a settings table where I wanted the whole cell to trigger the first responder for the cell's textfield.

I ended up adding a transparent (custom, blank title) button behind the label (touch disabled) and textfield after not getting any touches using gesture recognizers. I think it should work in a more elegant way, but it solved the task for now and that limited purpose. (and you can just drag connect from the button's default action)

Bit ugly. Then again, it just describes the area behind the text field reacting to touch. Which was the intention after all. So maybe its just not that fancy.

Will keep it until I find the reason for the recognizers not firing.

Upvotes: 0

surfrider
surfrider

Reputation: 1425

Problem in OHAttributedLabel. This label also handles tap on links. So for handle tap on any point of label (not just link) you must

self.textLabel.onlyCatchTouchesOnLinks = NO;

Where self.textLabel is your OHAttributedLabel. And don't forget of userInteractionEnabled.

Upvotes: 1

dawid
dawid

Reputation: 374

I don't know if it is the same problem but... I added a label and could not get it to recognize a touch, I eventually realised that it was because I was adding it as a subview, but its frame was outside its parent's frame, hence the touch heirarchy broke

Upvotes: 0

user189804
user189804

Reputation:

A UILabel isn't a UIControl so you won't get events on UIControlEventTouchUpInside or similar. Why not use a button instead? You can make it look exactly like a label.

Regardless you will probably need to set addTarget:action:forControlEvents: and tag on the UIButton in your cellForRowAtIndexPath: method. In that method, detect which cell's button was tapped by examining the tag value.

If you must use UILabel then you need to subclass it and intercept the touchesBegan/touchesEnded methods (inherited from UIResponder) to detect UIControlEventTouchUpInside yourself.

Upvotes: 1

Praveen-K
Praveen-K

Reputation: 3401

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
      UITouch *touch = [[event allTouches] anyObject];
      if (CGRectContainsPoint([self.site frame], [touch locationInView:self.view])){
       //do whatever you want
     }
}

Or

UILabel *label = =[UILabel alloc]init];
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture =
[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap)]     autorelease];
[label addGestureRecognizer:tapGesture];

Upvotes: 13

Related Questions