mcfly soft
mcfly soft

Reputation: 11645

how to call method in controller from a UIButton in a UITableViewCell

I have a UITableView with my own UITableViewCells, which contains a UIButton. The UITableView is added to my ViewController at runtime. Everything works fine that way that I see the Button in the Cells. When pressing the Button my action is called in my UITableViewCell.

Now. How can I call a method in my ViewController, when the button is pressed in my cell ?

In my UITableViewCell Class I have a button in a cell:

let framebutton_in_cell:CGRect=CGRectMake(0, 0, 40,40);
    button_in_cell.frame=framebutton_in_cell;
    button_in_cell.addTarget(self, action: "buttonActionText:", forControlEvents: UIControlEvents.TouchUpInside)
    button_in_cell.tag = 1;
    var image2 = UIImage(named: "book");
    button_in_cell.setImage(image2, forState: .Normal)
    var transparent2:UIColor=UIColor(red: 0, green: 0, blue: 0, alpha: 0.0);

    contentView.addSubview(button_in_cell);

and I have my function buttonActionText:

func buttonActionTimeline(sender:UIButton!)
{
    var btnsendtag:UIButton = sender
    //do something
}

My UITableView is added at runtime to my UIViewController:

 override func viewDidLoad() {
        super.viewDidLoad()
        let fSearchResultY:CGFloat=fButtonHeight+fButtonY;
        searchResults.frame = CGRectMake(0, fSearchResultY, screenwidth, screenheight-fSearchResultY);
        searchResults.delegate      =   self;
        searchResults.dataSource    =   self;
        searchResults.registerClass(CellResult.self, forCellReuseIdentifier: "Cell");
        self.view.addSubview(searchResults);

Now I would like to call a method in my Viewcontroller insteed of a method in my UITableViewCell. How can I achieve this ?

Upvotes: 1

Views: 2511

Answers (4)

Kalpesh
Kalpesh

Reputation: 5334

Create a subclass of UITableviewCell. In my code i have used Question Cell

 func  buttonActionTimeline(sender: UIButton!)

{
    var btn:UIButton=sender as UIButton

    let point = tblVIew.convertPoint(CGPointZero, fromView: sender)
    let indexPath : NSIndexPath = tblVIew.indexPathForRowAtPoint(point)!

    let cell = tblVIew.cellForRowAtIndexPath(indexPath) as QuestionCell // Your custom cell class name
    cell.loaddata() // add this method in to custom cell class
 }

Upvotes: 0

Zell B.
Zell B.

Reputation: 10286

Just don't add target from UITableViewCell class instead add it in cellForRowAtIndexPath method

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath) as! SomeCustomCell

    cell.button_in_cell.addTarget(self, action: "viewControllerMethod:", forControlEvents: UIControlEvents.TouchUpInside)

}

Upvotes: 4

Vizllx
Vizllx

Reputation: 9246

A very simple solution :-

  1. Just declare delegate method in your Custom cell.
  2. Set the delegate of Cell with the View Controller cell.delegate=self;
  3. Now call the delegate method on Cell's button action and also send the button.tag as object(if needed) , it will directly call your View Controller's delegate method which is defined .

Upvotes: 2

Mukesh
Mukesh

Reputation: 3690

I didn't got your question properly ?Is this u r looking for

func buttonActionText(sender:AnyObject){
    CGPoint buttonPosition=sender.convertPoint(CGPointZero, fromView: self.tableView)
    NSIndexPath  indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
}

Upvotes: 0

Related Questions