Swift:如何通过单击按钮 UITableVieCell 创建 UIActionSheet

编程入门 行业动态 更新时间:2024-10-25 14:28:06
本文介绍了Swift:如何通过单击按钮 UITableVieCell 创建 UIActionSheet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个带按钮的 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屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 11:15:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1408804.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   按钮   Swift   UITableVieCell   UIActionSheet

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!