【问题标题】:Swift: How to create UIActionSheet with click on button UITableVieCellSwift:如何通过单击按钮 UITableVieCell 创建 UIActionSheet
【发布时间】:2018-02-14 09:38:30
【问题描述】:

我有一个带有按钮的表格视图,当我单击 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!;
}

【问题讨论】:

标签: ios swift uitableview tableview


【解决方案1】:

试试这个并在 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!;
}

【讨论】:

    【解决方案2】:

    在单元格类中制作按钮的出口

    然后在您使用此单元格的 tableView 中,在cellForRowAtIndexPath 中编写以下代码

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

    现在在您的 yourButtonTapped 方法中,按照链接编写 actionSheet 代码:

    UIActionSheet iOS Swift

    希望有帮助

    【讨论】:

      【解决方案3】:

      关闭方法

      1 - 在你的 UserTableViewCell 中声明你的 actionBlock

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

      2 - 在您的 Cell Action 中执行您的操作块

      @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
      
      }
      

      表格视图单元格

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

      【讨论】:

        【解决方案4】:

        只需在 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
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-10-04
          • 2012-08-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-05-15
          • 2016-03-01
          相关资源
          最近更新 更多