简体   繁体   中英

tapping on table view cell

override func tableView(tableView: UITableView, cellForRowAtIndexPath 
indexPath : NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier ("ChecklistItem") as UITableViewCell
        let label = cell.viewWithTag(1000) as UILabel

        if indexPath.row == 0 {
            label.text = "jaswanth"
        } else if indexPath.row == 1 {
            label.text = "nikhil"

        } else if indexPath.row == 2 {
            label.text = "krishna"
        }


        return cell
    }

I have UITableViewController , I have used above code to display the cells in the table and I had another UIViewController when user taps on jaswanth the text(labels text) in UIViewController have be changed and the same way when nikhil is tapped the text(labels text) in UIViewController have changed

I had only one UITableViewController and one UIViewController how can I do this?

You would want to be able to detect when these cells in the table are pressed. This can be done using the recommendation by Mike Welsh through implementing the method:

func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
This allows you to be able to tell which cell is selected from the NSIndexPath.

From here on, it seems to me that you want to be able to change the text (on a button or field) that is controlled by another UIViewController. One way it can be done is to have a reference to this UIViewController and creating a method to update this text field. If the UITableViewController is a child view controller, it can be as simple as calling

override func prepareForSegue(_ segue: UIStoryboardSegue, sender sender: AnyObject?) {
    if segue.identifier == "SomeStringID" {
        self.tableViewController = segue.destinationViewController as? UITableViewController
        self.tableViewController.delegate = self
    }
}

Else if the Controllers are related by a Segue, it can also be done by overriding prepareForSegue of the original ViewController. By using the segue.identifier to map to the segue you have made in StoryBoard/programmatically, you are able to set a delegate/pointer to the original UIViewController.

 override func prepareForSegue(_ segue: UIStoryboardSegue, sender sender: AnyObject?) { \n    if segue.identifier == "SomeStringID" { \n        self.tableViewController = segue.destinationViewController as?  UITableViewController \n        self.tableViewController.delegate = self \n    } \n}  

With the reference to the other ViewController, you should be able to define your own method to change this text field quite easily

Hope this helps you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM