0

I've a custom table view cell which has a label view. I added tapGesture to invoke a function when that view is clicked. Currently my custom view cell is in it's own swift file. I added following code to enable 'Share extension' when clicked on that label.

  CustomCellView.swift

  10 myLabel.isUserInteractionEnabled = true
  11 let tap = UITapGestureRecognizer(target: self, action: #selector(tapLabelGesture))
  12 myLabel.addGestureRecognizer(tap)

  13 func tapLabelGesture() {
  14   print("Clicked on Label ")
       let url="www.google.com"
  15   let activityVC = UIActivityViewController(activityItems: [url], applicationActivities: nil)

  16   activityVC.popoverPresentationController?.sourceView = self.view
  17   self.present(activityVC, animated: true, completion: nil)
  18 }

I get compiler error on line 16 for self.view and 17 for self.present(). Question is how do I provide view for the popup?

This code I used for another view (without table view or cell) as a test and it worked fine. So I'm trying do the same technique for a tableview/cell. How do I resolve this issue? Any help is appreciated.

1
  • what does the compiler say? Commented Dec 16, 2017 at 3:12

3 Answers 3

1

For line 16:

You are getting an error which says your class CustomCellView has no member view because your class is subclass of UITableViewCell not UIViewController because UIViewController has that property and your CustomCellView have contentView for that.

For line 17:

same as above your class is not subclass of UIViewController thats why you can not use self.present for that.

Solution:

Instead of using UITapGestureRecognizer you can use UIButton which you can place on UILabel and in your UIViewController class add button.tag and button.addTarget in your cellForRowAt method.

Then you can add your code in your button method and present UIActivityViewController.

Hope this will help.

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't do it with button but gave me an idea to add tap gesture in cellForRowAt() method for the label itself (since it exists) in each cell.Which solved this problem. So Thanks for the suggestion. There is a connected problem of how do I read the value of the label (the value is dynamic for the label in each cell). This is definitely next logical question but not sure if I should open it as a new question?
0

As @DharmeshKheni mentioned you cannot use a subclass of UITableViewCell like UIViewController. It doesn't provide view property and present method.

Answering your question, you could store a closure in the CustomCellView:

var onLabelTappedCallback: (() -> Void)?

call it in your selector:

@objc private func tapLabelGesture() {
    onLabelTappedCallback?()
}

and finally implement in the cellForRowAt method:

cell.onLabelTappedCallback = {
    print("Label tapped")
    //Present additional vc
}

This solution will work with UIButton as well.

3 Comments

How do I call onLabelTappedCallback() from tapLabelGesture(), Since the definition is in cellForRowAt(). It's a compiler error with "unresolved identifier" error.
I'm not quite following you. In cellForRowAt method you only assign a closure to public CustomCellView's property onLabelTappedCallback. In CustomCellView class you call it onLabelTappedCallback?(). I have created project to test it you could check it here. I hope this will help to clarify your doubts.
This looks like it could work as well. I'll try it out. Thanks.
0

I got more ideas from this thread on SO, how to recognize label click on custom UITableViewCell - Swift 3 With the an extension, I was able to solve this issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.