1

I have Two Class One is ViewController and second is UITableViewCell IN tableview Cell I have created one Tapable link label . Label Delegates method to call my protocol method but its not working Here is my code 1)ViewController

public protocol DataEnteredDelegate: class { func userDidEnterInformation(info: NSString) } class ChatViewController: UIViewController{ override func viewDidLoad() { } func userDidEnterInformation(info: NSString) { print(info) } } 

2)UITableViewCell

class UIChatBubbleTableViewCell: UITableViewCell,TapLabelDelegate { var delegate_of_link:DataEnteredDelegate? = nil func tapLabel(tapLabel: TapLabel, didSelectLink link: String) { print(link) if (delegate_of_link != nil) { delegate_of_link!.userDidEnterInformation(link) } } } 

Where I am doing a mistake if Protocol is not working then I have to use Notification Center Please, give me some solution.

3
  • provide sufficient code to properly address the issue. you must set cell.delegate_of_link = <your_view_controller> have you done that? Commented Feb 26, 2016 at 5:56
  • tapLabel this is another function not a cell function then how i get cell. delegate_of_link , Commented Feb 26, 2016 at 6:01
  • In your second code snippet you have delegate_of_link = nil that should be assigned some valid value. I assume you are creating some cell in your view controller snippet 1. so there after creating a cell you must assign self to delegate_of_link so the method you defined in view controller gets called. Before doing that i suggest you check some tutorials on creating custom protocols. Commented Feb 26, 2016 at 6:10

1 Answer 1

1

I think you're implementing the delegate backwards. The UIViewController should be declaring itself the delegate of the cell, so that it responds to a call from the cell. So instead, it should look like this:

//TableViewCell public protocol DataEnteredDelegate: class { func userDidEnterInformation(info: NSString) } class UIChatBubbleTableViewCell: UITableViewCell{ internal var delegate: DataEnteredDelegate? { func tapLabel(tapLabel: TapLabel, didSelectLink link: String) { print(link) self.delegate?.userDidEnterInformation(link) } } //UIViewController class ChatViewController: UITableViewController, DataEnteredDelegate{ override internal func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = self.tableView.dequeueReusableCellWithIdentifier("identifier", forIndexPath: indexPath) as!UIChatBubbleTableViewCell cell.delegate = self return cell } func userDidEnterInformation(info: NSString) { //do thing } } 
Sign up to request clarification or add additional context in comments.

3 Comments

This Code return a error cell.delegate_of_link = self Error is "Cannot assign value of type ChatviewControler to type Data EnteredDelegated?"
Make sure you declared that the Controller implements the delegate protocol, like this class ChatViewController: UITableViewController, DataEnteredDelegate
You're welcome. If you don't mind could you accept my answer? You can modify your delegate method to include the sender, and then use various methods to determine the sender. One of the easiest might be to set the cell's tag: cell.tag = 1 and then check its tag to see which it came from.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.