简体   繁体   English

递归UITableViewCell按钮操作

[英]Recursive UITableViewCell button Action

I'm able to generate a UITableView and a UITableViewCell however my action is not working properly. 我可以生成UITableViewUITableViewCell但是我的操作无法正常工作。 My use case is similiar to this video 我的用例类似于该视频

I was using this question as a reference and was uncesscessful 我以这个问题为参考,没有必要

What am I missing? 我想念什么? Something swift 3 related? 迅捷3相关的东西吗?

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    var categories = ["foo", "bar"]

    func logAction(sender: UIButton!){
        print("HIT BUTTON YO")
    }

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.categories.count
    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {      
        let cell:CustomCell = tableView.dequeueReusableCell(withIdentifier: "categories", for: indexPath) as! CustomCell
        cell.label.text = categories[indexPath.row]
        let button = cell.button
        button?.tag = indexPath.row
        button?.addTarget(self, action: #selector(self.logAction), for: .touchUpInside)
        return(cell)
    }
}


class CustomCell: UITableViewCell {

    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var label: UILabel!


    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        //Configure view for selected state
    } 
}

There is a smarter way to handle a button action in a custom cell. 在自定义单元中有一种更聪明的方式来处理按钮动作。

In the custom cell create a callback closure with no parameter / no return value and an IBAction connected to the button. 在自定义单元格中,创建一个没有参数/没有返回值且将IBAction连接到按钮的回调闭包。 In the action call the callback 在动作中调用回调

class CustomCell: UITableViewCell {

    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var label: UILabel!

    var callback : (()->())?

    @IBAction func logAction(_ sender : UIButton) {
        callback?()
    }
}

In cellForRow assign a closure to the callback, the index path is captured. cellForRow ,为回调分配一个闭包,捕获索引路径。

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "categories", for: indexPath) as! CustomCell
    cell.label.text = categories[indexPath.row]
    cell.callback = {
        print("Button pressed", indexPath)
    }
    return cell
}

Note: return is not a function (no parentheses). 注意: return不是函数(不带括号)。

Try accessing the cell's button directly. 尝试直接访问单元格的按钮。 Instead of 代替

let button = cell.button
button?.tag = indexPath.row
button?.addTarget(self, action: #selector(self.logAction), for: .touchUpInside)

Set the properties on the button directly. 直接在按钮上设置属性。

cell.button.tag = indexPath.row
cell.button.addTarget(self, action: #selector(self.logAction), for: .touchUpInside)

是的,所以我只是删除了视图控制器并重新制作,它现在可以工作了__(ツ)_ /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自定义UITableViewCell按钮动作? - Custom UITableViewCell button action? 从UITableViewCell到ViewController的按钮操作 - Button Action from UITableViewCell to ViewController 重置UITableViewCell复选标记以进行按钮操作 - Reset UITableViewCell checkmarks For Button Action 自定义 UITableViewCell 中的按钮操作会影响其他单元格 - Button action in custom UITableViewCell affects other cells 如何在UITableViewCell中的按钮及其操作方法 - How to wire a Button in a UITableViewCell with its action method 如何通过按钮操作(带有参数)从自定义UITableViewCell推送视图 - How to push a view from custom UITableViewCell with button action (with parameters) 让UITableViewCell按钮操作返回未处理状态以触发tableView:didSelectRowAtIndexPath - Have UITableViewCell button action return as unhandled to trigger tableView:didSelectRowAtIndexPath UITableViewCell:dequeueReusableCellWithIdentifier:在特定操作上更改每个单元格的按钮背景 - UITableViewCell: dequeueReusableCellWithIdentifier: Change button background per cell on a certain action iOS - 无法从UITableViewCell中的动态创建按钮调用操作 - iOS - Unable to call action from dynamically created button in UITableViewCell 是否无法在 UITableViewCell 上的删除操作(滑动按钮)中使用自定义视图? - Is it impossible to use custom view in delete action ( swipe button ) on UITableViewCell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM