How to detect Cell selection in UITableView - Swift

前端 未结 3 988
失恋的感觉
失恋的感觉 2020-12-28 16:49

just wondering how I would go about implementing didSelectRowAtIndexPath or something similar into my app. I have a populated Table View with several dynamic cells and basi

3条回答
  •  天命终不由人
    2020-12-28 17:04

    This is how I managed to segue from UITableView cells to other view controllers after implementing cellForRow, numberOfRowsInSection & numberOfSectionsInTable.

    //to grab a row, update your did select row at index path method to:
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
        NSLog("You selected cell number: \(indexPath.row)!");
    
        if indexPath.row == 1 {
            //THE SEGUE 
            self.performSegue(withIdentifier: "goToMainUI", sender: self)
        }
    }
    

    Will output: You selected cell number: \(indexPath.row)!

    Remember to match the identifier of your segue in story board to the identifier in the function, e.g goToMainUI.

提交回复
热议问题