簡體   English   中英

Swift:如何通過單擊UITableVieCell按鈕創建UIActionSheet

[英]Swift: How to create UIActionSheet with click on button UITableVieCell

我有一個帶有按鈕的表格視圖,當我單擊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!;
}

封閉方法

1-在UserTableViewCell中聲明actionBlock

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

2-在單元動作中執行動作塊

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

3-設置您的單元格塊操作,以調整cellForRowAtIndexPath方法

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

}

完整代碼

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

}

TableView單元格

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

    }

}

只需在ViewControler中添加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中,在cellForRowAtIndexPath編寫以下代碼

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

現在,在yourButtonTapped方法中,在鏈接之后編寫actionSheet代碼:

UIActionSheet iOS Swift

希望它的幫助

嘗試此操作並在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!;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM