【问题标题】:How to run a function when tableviewcell is selected (for example, update UIImageView when cell is pressed)选择 tableviewcell 时如何运行函数(例如,按下单元格时更新 UIImageView)
【发布时间】:2017-03-17 05:55:56
【问题描述】:

我正在制作一个应用程序,该应用程序有一个集合表视图,当将单元格选择单元格将其SEGUS分成另一个视图控制器时,具有TableView。

我想使用这个 tableview 来更新我的 VC 中的 UIImageView。因此,假设我在 TBC 上按“仰卧起坐”,我希望图像视图更新为显示仰卧起坐的动画。希望有人能理解我想做什么,谢谢你的帮助!

【问题讨论】:

    标签: ios swift uitableview uiimageview


    【解决方案1】:

    当点击单元格时调用委托方法tableView(_:didSelectRowAt:)
    在此方法中,您可以将逻辑插入到另一个 viewController 或更新同一 viewController 中的 imageView

    对于 Swift 3.0:

    override  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      //your code...
    }
    

    文档:https://developer.apple.com/reference/uikit/uitableviewdelegate/1614877-tableview

    【讨论】:

      【解决方案2】:

      你的问题很简单。 使用方法。

                       tableView(_:didSelectRowAt:)
      

      然后把你的方法放在这里做选择,你可以导航到你想要的视图控制器。

      【讨论】:

        【解决方案3】:

        你可以试试这个:

        override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let cell = tableView.cellForRow(at: indexPath) as UITableViewCell
            //so you can do whatever you want on your cell
        
        
        }
        

        【讨论】:

          【解决方案4】:

          是的,您可以使用 Tableview 的 didSelect 委托方法触发单元格的任何操作,但是...

          我认为您正在尝试做的是从另一个 VC 中进行更新?是这样的话,你需要的是使用委托或 NSNotificationCenter 在类之间发送更新。

          【讨论】:

            猜你喜欢
            • 2015-12-25
            • 1970-01-01
            • 1970-01-01
            • 2021-10-07
            • 2018-01-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多