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

     2023-02-27     110

关键词:

【中文标题】Swift:如何通过单击按钮 UITableVieCell 创建 UIActionSheet【英文标题】:Swift: How to create UIActionSheet with click on button UITableVieCell 【发布时间】: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!;

【问题讨论】:

查看这个答案***.com/questions/48740651/… @Reinier 在目标 c 中回答 这是客观的c.. 您需要使用委托模式。检查这个:***.com/questions/39585638/… 【参考方案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

【讨论】:

如何通过单击按钮在 Swift UI 中从一个视图导航到另一个视图

】如何通过单击按钮在SwiftUI中从一个视图导航到另一个视图【英文标题】:HowtonavigatefromoneviewtoanotherinSwiftUIonaclickofbutton【发布时间】:2019-11-2105:45:03【问题描述】:我想通过单击按钮在SwiftUI中从一个视图推送到另一个视图。同... 查看详情

如何通过在 swift (Xcode) 中单击子视图内的按钮来更改父视图

】如何通过在swift(Xcode)中单击子视图内的按钮来更改父视图【英文标题】:Howtochangeaparentviewbyclickingabuttoninsideofsubviewinswift(Xcode)【发布时间】:2017-03-2323:22:45【问题描述】:我正在开发一个需要菜单栏的应用程序,它应该在许多... 查看详情

如何通过单击swift4中的addImage按钮将图像一张一张添加到tableviewcell中

】如何通过单击swift4中的addImage按钮将图像一张一张添加到tableviewcell中【英文标题】:HowtoaddimagesonebyoneintotableviewcellbyclickingaddImagebuttoninswift4【发布时间】:2019-09-2407:31:52【问题描述】:importUIKitclassViewController:UIViewController,UITable... 查看详情

如何在 Swift 中的 UITableViewCell 上添加带有单击事件的按钮?

】如何在Swift中的UITableViewCell上添加带有单击事件的按钮?【英文标题】:HowtoaddabuttonwithclickeventonUITableViewCellinSwift?【发布时间】:2015-05-0715:15:08【问题描述】:在我的主页中,我为UITableViewCell创建了一个xib文件。我正在从该xib... 查看详情

如何在 swift 中单击按钮时更改 uiimage 的图像?

】如何在swift中单击按钮时更改uiimage的图像?【英文标题】:howtochangeuiimage\'simageonbuttonclickinswift?【发布时间】:2014-10-0408:52:04【问题描述】:我正在尝试通过快速单击按钮来更改uiimage控件的包含图像。为此,我编写了一个简单... 查看详情

swift:通过单击按钮与蓝牙设备配对

】swift:通过单击按钮与蓝牙设备配对【英文标题】:swift:pairwithbluetoothdevicewithbuttonclick【发布时间】:2018-02-2620:39:17【问题描述】:到目前为止,当应用程序打开时iPhone已与蓝牙设备配对,但我想通过单击按钮来配对。我试图... 查看详情

如何通过单击按钮转到另一个视图

】如何通过单击按钮转到另一个视图【英文标题】:Howtogotoanotherviewwithbuttonclick【发布时间】:2019-08-0115:01:20【问题描述】:我的代码中有一个按钮,我有一个名为LogindView.swift的文件单击按钮时,我无法获取打开另一个视图文件... 查看详情

通过单击 swift 中的按钮设置选定的行

】通过单击swift中的按钮设置选定的行【英文标题】:setselectedrowbyclickingbuttoninswift【发布时间】:2017-09-1715:00:27【问题描述】:我有一个按钮的表格视图和单元格。我想当我点击哪一行的按钮时,当前行被选中。(我的意思是行... 查看详情

Swift:如何在单击提交按钮时更改 UIButton 图像

】Swift:如何在单击提交按钮时更改UIButton图像【英文标题】:Swift:HowtoChangeUIButtonImageonclickingsubmitbutton【发布时间】:2016-08-2806:51:38【问题描述】:我有四个UIButtons作为复选框,点击按钮时,图像会发生变化。我有一个提交按钮... 查看详情

如何检测用户何时单击 lyft 按钮 swift

】如何检测用户何时单击lyft按钮swift【英文标题】:howtodetectwhenuserclicklyftButtonswift【发布时间】:2019-05-2312:18:54【问题描述】:我们需要Lyft按钮触摸事件,因为我从事分析工作,所以,我需要有多少人选择Lyft,但我不能放置UIVi... 查看详情

iOS,Swift 4,导航控制器标题颜色在通过单击后退按钮返回时不会更改

】iOS,Swift4,导航控制器标题颜色在通过单击后退按钮返回时不会更改【英文标题】:iOS,Swift4,NavigationControllertitlecolourisnotbeingchangedwhencomingbackviaclickingonbackbutton【发布时间】:2018-07-3123:17:41【问题描述】:我的问题与这个问题Vie... 查看详情

swift:如何在单击按钮上对所有单元格进行 TTS

】swift:如何在单击按钮上对所有单元格进行TTS【英文标题】:swift:howtoTTSallcellonclickbutton【发布时间】:2018-02-0610:03:57【问题描述】:我在多个单元格的UITableViewCell中有一个UITextView:在单击UIButton时,需要所有单元格textView的文... 查看详情

如何在 swift UI 中单击按钮时从 swift UI 导航到情节提要?

】如何在swiftUI中单击按钮时从swiftUI导航到情节提要?【英文标题】:HowtonavigatefromswiftUItostoryboardonbuttonclickinswiftUI?【发布时间】:2021-08-0112:12:51【问题描述】:我正在开发一个同时具有swiftUI和情节提要的应用程序。我在swiftUI中... 查看详情

如何在swift 3中的tableview单元格内单击按钮标题

】如何在swift3中的tableview单元格内单击按钮标题【英文标题】:howtochangethebuttontitleonclickinsideatableviewcellinswift3【发布时间】:2017-08-3007:42:47【问题描述】:我有一个tableview单元格,里面有图像视图、标签和一个ui按钮,我需要在... 查看详情

如何检查是不是从 Swift 的 XIB 文件中单击了按钮?

】如何检查是不是从Swift的XIB文件中单击了按钮?【英文标题】:HowtocheckifbuttonisclickedfromXIBfileinSwift?如何检查是否从Swift的XIB文件中单击了按钮?【发布时间】:2017-11-0918:11:51【问题描述】:我遇到以下情况。我使用XIB文件创建... 查看详情

Facebook 登录:iOS/Swift - 单击按钮两次

...1-1716:38:42【问题描述】:还有其他人遇到过这个问题吗?通过xCode-iOS-Swift-使用Parse。我创建了一个按钮,点击后会触发注册Facebook流程。问题:我必须点击两次。第一次它会说呃哦。用户取消了Facebook登录,然后再次单击时,F 查看详情

通过单击按钮显示消息,但我如何通过单击按钮隐藏此消息

】通过单击按钮显示消息,但我如何通过单击按钮隐藏此消息【英文标题】:showmessagebyclickingbuttonbuthowicanhidethismessagebyclickingthebutton【发布时间】:2019-10-2020:10:41【问题描述】:这是我的代码,当我单击它显示消息的按钮时,但... 查看详情

如何使用swift将功能传递给网页?

】如何使用swift将功能传递给网页?【英文标题】:Howtopassfunctiontowebpageusingswift?【发布时间】:2019-03-1805:24:33【问题描述】:我有一种情况,我需要通过网页中的按钮单击来捕获事件。此代码在网页上单击按钮。okCoolClick=()=>/*... 查看详情