How to detect tableView cell touched or clicked in swift

后端 未结 9 874
灰色年华
灰色年华 2020-12-07 21:46

I\'m trying to get index of selected item in TableView and start some activity after that. Unfortunately most of solutions that I found are in obje

相关标签:
9条回答
  • 2020-12-07 22:16

    This worked good for me:

        override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            print("section: \(indexPath.section)")
            print("row: \(indexPath.row)")
        }

    The output should be:

    section: 0
    row: 0
    
    0 讨论(0)
  • 2020-12-07 22:22

    Inherit the tableview delegate and datasource. Implement delegates what you need.

        override func viewDidLoad() {
            super.viewDidLoad()
            tableView.delegate = self
            tableView.dataSource = self
        }
    

    And Finally implement this delegate

         func tableView(_ tableView: UITableView, didSelectRowAt  
         indexPath: IndexPath) {
         print("row selected : \(indexPath.row)")
      }
    
    0 讨论(0)
  • 2020-12-07 22:25
     # Check delegate? first must be connected owner of view controller
    
        # Simple implementation of the didSelectRowAt function.
    
        func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
             print("row selection: \(indexPath.row)")
        }
    
    0 讨论(0)
  • 2020-12-07 22:32

    If you want the value from cell then you don't have to recreate cell in the didSelectRowAtIndexPath

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        println(tasks[indexPath.row])
    }
    

    Task would be as follows :

    let tasks=["Short walk",
        "Audiometry",
        "Finger tapping",
        "Reaction time",
        "Spatial span memory"
    ]
    

    also you have to check the cellForRowAtIndexPath you have to set identifier.

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as UITableViewCell
        var (testName) = tasks[indexPath.row]
        cell.textLabel?.text=testName
        return cell
    }
    

    Hope it helps.

    0 讨论(0)
  • 2020-12-07 22:33

    To get an elements from Array in tableView cell touched or clicked in swift

    func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as UITableViewCell
        cell.textLabel?.text= arr_AsianCountries[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let indexpath = arr_AsianCountries[indexPath.row]
    print("indexpath:\(indexpath)")
    }
    
    0 讨论(0)
  • 2020-12-07 22:37

    A of couple things that need to happen...

    1. The view controller needs to extend the type UITableViewDelegate

    2. The view controller needs to include the didSelectRowAt function.

    3. The table view must have the view controller assigned as its delegate.


    Below is one place where assigning the delegate could take place (within the view controller).

    override func loadView() {
        tableView.dataSource = self
        tableView.delegate = self
        view = tableView
    }
    

    And a simple implementation of the didSelectRowAt function.

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("row: \(indexPath.row)")
    }
    
    0 讨论(0)
提交回复
热议问题