Swift:如何通过单击按钮 UITableVieCell 创建 UIActionSheet [英] Swift: How to create UIActionSheet with click on button UITableVieCell

查看:18
本文介绍了Swift:如何通过单击按钮 UITableVieCell 创建 UIActionSheet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我的 UITableViewCell:

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

}

}

在我的视图控制器中:

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

推荐答案

试试这个并在 UserTableViewCell 中做一些改变

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

并修改这个方法

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

这篇关于Swift:如何通过单击按钮 UITableVieCell 创建 UIActionSheet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆