如何激活按钮以从 tableview 单元格(多个单元格)内部执行操作

Posted

技术标签:

【中文标题】如何激活按钮以从 tableview 单元格(多个单元格)内部执行操作【英文标题】:How to active button to do action from inside tableview cell (multiple cells) 【发布时间】:2018-01-02 16:51:07 【问题描述】:

如何在表格单元格内使(提供)按钮执行操作,如图所示: this picture of table view include suctions , cells then inside it the button ( make offer ) button

我正在使用此代码,但是当我单击它时会出错

错误: error

这是代码

    // tabels functions


public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    return 1



func numberOfSections(in tableView: UITableView) -> Int 
    return 5


func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? 

let view = UIView()

// make section tachuble

view.backgroundColor = UIColor.white

let SW = UIScreen.main.bounds.width

let Ordlbl = UILabel()
Ordlbl.text = "Header"
Ordlbl.frame = CGRect (x: 16, y: 0, width: SW - 16, height: 40)
Ordlbl.font = UIFont(name:"Courier-bold", size: 20.0)
view.addSubview(Ordlbl)

let fromlbl = UILabel()
fromlbl.text = "A12345678"
fromlbl.frame = CGRect (x: 16, y: 20, width: SW - 16, height: 40)
fromlbl.textAlignment = NSTextAlignment.left
fromlbl.font = UIFont(name:"Courier", size: 20.0)
view.addSubview(fromlbl)

let deslbl = UILabel()
deslbl.text = "- 20 KM ->"
deslbl.textAlignment = NSTextAlignment.center
deslbl.font = UIFont(name:"Courier", size: 20.0)
deslbl.frame = CGRect (x: 0, y: 20,width: SW - 16, height: 40)

view.addSubview(deslbl)

let tolbl = UILabel()
tolbl.text = "B12345678"
tolbl.textAlignment = NSTextAlignment.right
tolbl.font = UIFont(name:"Courier", size: 20.0)
tolbl.frame = CGRect (x: 0, y: 20, width: SW - 16, height: 40)
view.addSubview(tolbl)

return view

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat 
    return 60



public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 


    let cell = UITableViewCell()

    cell.backgroundColor = UIColor.init(red: 234/255, green: 238/255, blue: 237/255, alpha: 1.00)

    let SW = UIScreen.main.bounds.width

    let deslbl = UILabel()
    deslbl.text = "Order Description :"
    deslbl.textAlignment = NSTextAlignment.left
    deslbl.font = UIFont(name:"Courier-Bold", size: 17.5)
    deslbl.frame = CGRect (x: 16, y: 16, width: SW - 68, height: 40)
    cell.addSubview(deslbl)

    let deslbls = UILabel()
    deslbls.text = "fgwerhioghefrege gergu eriguerg erg uerg peor ger gey gerop gpoergrg er grgporg eropg yergy erpog regy reyg poreg oper goerygpre gyergoer goyer goer goy ergy erg er gyeogp yerop gyoery georgy eroy gpoer gyoer goepr gopre goper gpo ergo ery goper gyroepgy repo gyreopyg repogy peroyg peorg yerpog eropg yeropg yeropg yerop"
    deslbls.textAlignment = NSTextAlignment.left
    deslbls.lineBreakMode = .byWordWrapping
    deslbls.numberOfLines = 6
    deslbls.font = UIFont(name:"Courier", size: 15.0)
    deslbls.frame = CGRect (x: 16, y: 42, width: SW - 68, height: 100)
    cell.addSubview(deslbls)

    let Timelbl = UILabel()
    Timelbl.text = "60"
    Timelbl.textAlignment = NSTextAlignment.right
    Timelbl.font = UIFont(name:"Courier-Bold", size: 35.0)
    Timelbl.frame = CGRect (x: SW - 70, y: 16, width: 50, height: 50)
    cell.addSubview(Timelbl)

    let Timelbls = UILabel()
    Timelbls.text = "min"
    Timelbls.textAlignment = NSTextAlignment.right
    Timelbls.font = UIFont(name:"Courier-Bold", size: 10)
    Timelbls.frame = CGRect (x: SW - 82, y: 35, width: 50, height: 50)
    cell.addSubview(Timelbls)

    let textin = UITextField()
    textin.text = "20"
    textin.backgroundColor = UIColor.white
    textin.textAlignment = NSTextAlignment.center
    textin.font = UIFont(name:"Courier-Bold", size: 20)
    textin.frame = CGRect (x: 16, y: 150, width: SW - 124, height: 35)
    cell.addSubview(textin)

    let Btin = UIButton()
    Btin.backgroundColor = UIColor.init(red: 127/255, green: 192/255, blue: 0/255, alpha: 1.00)
    Btin.frame = CGRect (x: SW - 100, y: 150, width: 90, height: 35)
    Btin.tag = indexPath.row
    Btin.addTarget(self, action: Selector("printC"), for: UIControlEvents.touchUpInside)
    cell.addSubview(Btin)


    let Btinlbl = UILabel()
    Btinlbl.text = "Make Offer"
    Btinlbl.lineBreakMode = .byWordWrapping
    Btinlbl.numberOfLines = 2
    Btinlbl.textAlignment = NSTextAlignment.center
    Btinlbl.font = UIFont(name:"Courier-Bold", size: 15)
    Btinlbl.frame = CGRect (x: SW - 95, y: 150, width: 80, height: 35)
    cell.addSubview(Btinlbl)




    return cell


func printC()
    print("ok")



func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
    return 200


// end table

这段代码是我的问题:

        let Btin = UIButton()
    Btin.backgroundColor = UIColor.init(red: 127/255, green: 192/255, blue: 0/255, alpha: 1.00)
    Btin.frame = CGRect (x: SW - 100, y: 150, width: 90, height: 35)
    Btin.tag = indexPath.row
    Btin.addTarget(self, action: Selector("printC"), for: UIControlEvents.touchUpInside)
    cell.addSubview(Btin)

它的功能是这样的:

    func printC()
    print("ok")

【问题讨论】:

您的选择器不正确。尝试正确的。 为什么不选择storyBoard作为设计目的? 正确的解决方案是创建一个自定义表格视图单元格并使用委托。这应该会有所帮助:***.com/questions/43627355/… 好的,抱歉。下次会仔细看看@Moritz 【参考方案1】:

替换

Selector("printC")

#selector(YourViewController.printC)

然后替换

func printC()

@objc func printC()

【讨论】:

无法将类型 '(ViewController) -> () -> ()' 的值转换为预期的参数类型 'String' 你的代码 Btin.addTarget(self, action: Selector(ViewController.printC), for: UIControlEvents.touchUpInside) 应该是#selector 而不是Selector 现在我有 2 个错误:表达式列表中的预期表达式和调用 Insert 中参数“action”的缺少参数,操作:' 感谢它的工作 Btin.addTarget(self, action:#selector(printC), for: UIControlEvents.touchUpInside)【参考方案2】:

使用回调 在 Cell 类内部编写以下代码:

var buttonActionCallBack:(()-> Viod)?

 @IBAction func buttonAction(sender: UIButton) 
   if let call = buttonActionCallBack 
   call()
   

在 CellForRow 内部编写波纹管代码

cell.buttonActionCallBack = 
  // Action of Button

【讨论】:

我没有把它写在我的代码中? var buttonActionCallBack:(()-> Viod)? @IBAction func buttonAction(sender: UIButton) if let call = buttonActionCallBack call() 【参考方案3】:

在 Swift 3 中 在我的演示中,我有两个按钮

    完成按钮 取消按钮

在我的 tableview 单元格文件中,我创建了一种协议方法,显示在下面的代码中

@objc protocol MyCellDelegate : NSObjectProtocol

    @objc optional func didClickDoneButton()
     @objc optional func didClickCancleButton()

class MyBookCell: UITableViewCell

var delegate:MyCellDelegate?
   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
    

    //======================================================================
    //MARK:- Button Done Tapped
    //======================================================================

    @IBAction func btnDoneTapped(_ sender: UIButton)
    
        delegate?.didClickDoneButton?()
    
    @IBAction func btnCancleTapped(_ sender: UIButton)
    
       delegate?.didClickCancleButton?()
    


在视图控制器中

import UIKit


class MyViewController: UIViewController, UITableViewDelegate , UITableViewDataSource ,MyCellDelegate

@IBOutlet weak var MyTable: UITableView!
var MyArray:[String] = ["A" , "B","c"]

 override func viewDidLoad()
    
        super.viewDidLoad()


        self.MyTable.rowHeight = UITableViewAutomaticDimension
        self.MyTable.estimatedRowHeight = 80



    
 //===================================================================
    //MARK:- tableView Delegate Method
    //===================================================================

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 

        return MyArray.count
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 

        let cell = tableView.dequeueReusableCell(withIdentifier: "MyBookCell") as! MyBookCell
cell.delegate = self

        return cell




func didClickDoneButton()

// Your code here
// this is optional method if you don't use then no error found

func didClickCancleButton()

// Your code here
// this is optional method if you don't use then no error found


【讨论】:

以上是关于如何激活按钮以从 tableview 单元格(多个单元格)内部执行操作的主要内容,如果未能解决你的问题,请参考以下文章

Excel:如何创建快捷方式/浮动按钮以从工作表中的任何位置填充特定单元格?

在tableView中选择多个单元格并将另一个tableView单元格中的选定单元格显示为标签?

UIButton状态在用多个部分滚动tableview时改变 - Swift

如何在 tableView 单元格中实现收藏按钮?

如何通过单击单元格中的按钮来删除 tableView 中的单元格?使用核心数据

iOS如何从tableview单元格中获取按钮文本