【问题标题】:IOS swift 3 UITableViewIOS swift 3 UITableView
【发布时间】:2017-01-24 10:27:52
【问题描述】:

如何选择以编程方式选择的 UITableView 行数据。

例如。我这样开发选择

self.tableView.selectRow(at: indexPath, animated:true, scrollPosition: .middle)

为了获取我使用的行数据

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){

}

我没有得到结果。

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    在您的 viewDidLoad 中设置关注

     tableView.delegate = self 
    

    【讨论】:

      【解决方案2】:

      您可以通过以下方式获得它:

      func getCell(row: Int, section: Int) -> CellClassName {
              let indexPath = NSIndexPath(forRow:row, inSection: section)
              let selectedCell = tableView.cellForRowAtIndexPath(indexPath) as! CellClassName!
              return selectedCell
          }
      

      【讨论】:

        【解决方案3】:

        摘自Apple docs:

        调用此方法不会导致代理收到tableView(_:willSelectRowAt:) 或tableView(_:didSelectRowAt:) 消息,也不会向观察者发送UITableViewSelectionDidChange 通知。

        所以你的委托方法不会被调用。但是,如果您正在调用该方法,则您已经拥有所选元素的索引路径,因此您可以像在委托方法中一样处理它。

        例如:

        var elements: [String] = []
        var selectedElement: String?
        
        var tableView: UITableView!
        
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            selectedElement = elements[indexPath.row]
        }
        
        func selectElement(element: String) {
            let indexPath = IndexPath(row: elements.index(of: element), section: 0)
            selectedElement = element
            tableView.selectRow(at: indexPath, animated: true, scrollPosition: .middle)
        }
        

        【讨论】:

          【解决方案4】:

          您要查找的内容在UITableView 的Managing Selections 部分中定义:

          标识所选行的行和部分的索引路径。

          表示所选行的索引路径。

          【讨论】:

            猜你喜欢
            • 2017-11-26
            • 2017-05-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-03-21
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多