1

I have a tableview with buttons, and I would like to create a UIActionSheet here when I click on the 3 dots button. It is a custome tableview cell.

Look the image

My UITableViewCell:

import UIKit

class UserTableViewCell: UITableViewCell {


@IBOutlet weak var nameLabel: UILabel!

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
}

func view(with user: User){
    nameLabel.text = user.getName();
}

@IBAction func btnMenu(_ sender: UIButton) {
    //here I want to execute the UIActionSheet
}

@IBAction func btnDial(_ sender: UIButton) {

}

}

and in my view controller:

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "userCell", for: indexPath) as? UserTableViewCell;

    cell?.view(with: users[indexPath.row]);

    return cell!;
}
Raptor
  • 53,206
  • 45
  • 230
  • 366

4 Answers4

1

make outlets of button in cell class

then in tableView where you are using this cell write the code below in cellForRowAtIndexPath

  cell.yourButton.addTarget(self, action: #selector(yourButtonTapped), for: .touchDown)

and now in your yourButtonTapped method write actionSheet code following the link :

UIActionSheet iOS Swift

hope its help

1

Try this and do some changes in UserTableViewCell

class UserTableViewCell: UITableViewCell {
    weak var myVC : UIViewController?
    @IBAction func btnMenu(_ sender: UIButton) {
        //here I want to execute the UIActionSheet
        let actionsheet = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)

        actionsheet.addAction(UIAlertAction(title: "Take a Photo", style: UIAlertActionStyle.default, handler: { (action) -> Void in
        }))

        actionsheet.addAction(UIAlertAction(title: "Choose Exisiting Photo", style: UIAlertActionStyle.default, handler: { (action) -> Void in
        }))
        actionsheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: { (action) -> Void in

        }))
        myVC?.present(actionsheet, animated: true, completion: nil)
    }
}

And modify this method

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "userCell", for: indexPath) as? UserTableViewCell;

    cell?.view(with: users[indexPath.row]);
    cell?.myVC = self
    return cell!;
}
jignesh Vadadoriya
  • 3,244
  • 3
  • 18
  • 29
1

Closure Approach

1 - Declare your actionBlock in your UserTableViewCell

 var actionClosure : (()->Void)? = nil

2 - Execute your action block in your Cell Action

@IBAction func btnMenu(_ sender: UIButton) {
    //here I want to execute the UIActionSheet
    self.actionClosure?()
}

3 - Setup your cell block action adjusting your cellForRowAtIndexPath method

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "UserTableViewCell", for: indexPath) as! UserTableViewCell
    cell.actionClosure = { [weak self] in
        //SHow your ActionSheet Here
    }
    return cell

}

Full Code

CellForRow implementation

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "UserTableViewCell", for: indexPath) as! UserTableViewCell
    cell.actionClosure = { [weak self] in
        //SHow your ActionSheet Here
    }
    return cell

}

TableView Cell

import UIKit

class UserTableViewCell: UITableViewCell {

    @IBOutlet weak var nameLabel: UILabel!
    var actionClosure : (()->Void)? = nil

    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
    }

    func view(with user: User){
        nameLabel.text = user.getName();
    }

    @IBAction func btnMenu(_ sender: UIButton) {
        //here I want to execute the UIActionSheet
        self.actionClosure?()
    }

    @IBAction func btnDial(_ sender: UIButton) {

    }

}
Reinier Melian
  • 20,519
  • 3
  • 38
  • 55
0

just add onMenubtnClick method in your ViewControler instead of cell.

add this in your cellForRowAt method

cell.youtBtn.addTarget(self, action: #selector(self.btnMenu(_:)), for: .touchUpInside)

add this code in your ViewController

@IBAction func btnMenu(_ sender: UIButton) {
  //here I want to execute the UIActionSheet
}
Pratik Prajapati
  • 1,137
  • 1
  • 13
  • 26