Search code examples
uitableviewswift2ios9celluialertcontroller

How do I open a alertcontroller from a button in tableview cell in ios 9?


I want to open an Alertview with Call button and cancel button from button in a tableview cell.

var arrayOfUsernames: [PFObject] = [ ]

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrayOfUsers.count
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int{
    return 1
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "Tutors"
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell: CustomTableViewCell = TutorTable.dequeueReusableCellWithIdentifier("Tutor") as! CustomTableViewCell

    cell.CUsername.text = self.arrayOfUsers[indexPath.row]
    cell.Classes.text = self.arrayOfClasses[indexPath.row]
    return cell

}

here is the customtableviewcell

import UIKit

class CustomTableViewCell: UITableViewCell {

    @IBOutlet weak var CUsername: UILabel!
    @IBOutlet weak var Distance: UILabel!
    @IBOutlet weak var Classes: UILabel!
    @IBOutlet weak var Rating: UIImageView!

    @IBAction func bookNow(sender: AnyObject) {
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

Solution

  • Since UITableViewCell is a UIView subclass, and not UIViewController, you'll need to create a variable that holds a viewController you can present from.

    First, you want to add a weak viewController var to your CustomTableViewCell. I recommend using a navigationController simply because you can use that to pushViewController: if you need it for other things, and you can control the stack from it if you need. If not, you can just use the UITableViewController.

    In your cellForRowAtIndexPath: method, simply set the navigationController variable in your cell class to the current navigationController or the tableViewController:

    cell.navigationController = self.navigationController or... cell.viewController = self

    Once you do that, in your bookNow: method, you can now use a UIViewController to present the view. Here's what the code looks like...

    Your UITableViewController class:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) → UITableViewCell? {
      let cell = tableView.dequeueReusableCellWithIdentifier("Tutor") as! CustomTableViewCell
      // setup your cell here
      cell.navigationController = self.navigationController
      // cell.viewController = self
      return cell
    }
    

    In your CustomCellClass:

    weak var navigationController: UINavigationController?
    // weak var viewController: UIViewController?
    
    @IBAction func bookNow(sender: AnyObject) {
      let alertController = UIAlertController(/* setup */)
      // setup alertController
      self.navigationController?.presentViewController(alertController, animated: false, completion: nil)
      // self.viewController?.presentViewController(alertController, animated: false, completion: nil)
    }
    

    On a side note, I strongly advice changing your variable names to lower cases. Upper cases are usually used for Class names