Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift:如何使用单击按钮UITableVieCell创建UIActionSheet_Ios_Swift_Uitableview_Tableview - Fatal编程技术网

Ios Swift:如何使用单击按钮UITableVieCell创建UIActionSheet

Ios Swift:如何使用单击按钮UITableVieCell创建UIActionSheet,ios,swift,uitableview,tableview,Ios,Swift,Uitableview,Tableview,我有一个带有按钮的tableview,当我点击3点按钮时,我想在这里创建一个UIActionSheet。它是一个自定义的tableview单元格 我的UITableViewCell: import UIKit class UserTableViewCell: UITableViewCell { @IBOutlet weak var nameLabel: UILabel! override func awakeFromNib() { super.awakeFromNib()

我有一个带有按钮的tableview,当我点击3点按钮时,我想在这里创建一个UIActionSheet。它是一个自定义的tableview单元格

我的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) {

}

}
在我的视图控制器中:

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!;
}

只需在ViewController中添加onMenubtnClick方法,而不是单元格

将此添加到您的
cellForRowAt
方法中

cell.youtBtn.addTarget(self, action: #selector(self.btnMenu(_:)), for: .touchUpInside)
在ViewController中添加此代码

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

在单元类中制作按钮的插座

然后在使用此单元格的tableView中,在
cellforrowatinexpath

  cell.yourButton.addTarget(self, action: #selector(yourButtonTapped), for: .touchDown)
现在,在您的
yourButtonTapped
方法中,按照链接编写操作表代码:


希望它有帮助

试试这个,在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)
    }
}
并修改此方法

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!;
}
闭合法 1-在UserTableViewCell中声明actionBlock

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) {

    }

}
2-在单元格操作中执行操作块

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

}
3-设置单元格块操作,调整cellForRowAtIndexPath方法

完整代码

CellForRow实施

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

}
表格视图单元格

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) {

    }

}

在目标cit的目标c中选中此答案@Reinier答案。您需要使用委托模式。选中此项:
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) {

    }

}