【问题标题】:call multiple button in a UITableviewcell在 UITableviewcell 中调用多个按钮
【发布时间】:2018-06-16 19:45:31
【问题描述】:

我有一个带有 4 个按钮的 xib 文件,每个按钮都有一个功能,它必须在单击时启动。该函数必须获取数据,并且在获取数据后它应该执行一个segue。我只能使用 UITableViewCell 类型的 nib 并将其显示在 viewcontroller 内的 tableview 中。

在tableviewcell中点击时是否可以让每个按钮启动所需的功能,如果可以,我该怎么做。

我几天来一直在研究与我类似的问题,并尝试了所有建议的解决方案,但它们不起作用,例如

Show ViewController from XIB-file-Button - Swift

Perform segue from button in XIB

Swift - Segue from Button inside a cell

Perform segue from button in XIB

Perform Segue from Xib programmatically

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    当然,在代码中将事件传递到操作中然后访问索引路径非常简单。

    首先将目标添加到您的按钮。

    button.addTarget(self, action: #selector(someAction(_:event:)), for: .touchUpInside)
    

    然后使用此代码开始您的操作。

    @objc func someAction(_ button: UIButton, event: UIEvent) {
        guard let location = event.allTouches?.first?.location(in: tableView),
            let indexPath = tableView?.indexPathForItem(at: location)
            else {
                return
        }
    
        // TODO: fetch data
    
        performSegue(withIdentifier: "mySegueID", sender: button)
    }
    

    此外,如果您的数据是异步的,您可以使用 PromiseKit 之类的东西。 https://github.com/mxcl/PromiseKit

    【讨论】:

    • 函数在哪里,因为我获取数据的代码在 nib 类中
    • 你可以创建一个控制器对象(继承自 NSObject 或什么都不继承)并从那里调用它。
    • 像个视频模型?
    • 如果你的设计是基于MVVM的那么肯定,否则你可以简单地把它称为​​控制器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    相关资源
    最近更新 更多