S.M_Emamian
S.M_Emamian

Reputation: 17393

how handle click cell in UITableViewController

I would like to handle click each cell in my controller but my log doesn't show anything !

class RightMenuController: UITableViewController {


    let row_items = ["دسته بندی", "ثبت نام/ورود"]

    override func viewDidLoad() {
        navigationController?.navigationBar.isHidden = true
        super.viewDidLoad()

        self.tableView.delegate = self
        self.tableView.dataSource = self


        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return row_items.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)

        cell.textLabel?.text = row_items[indexPath.row]


        return cell

    }


    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {


        print(self.row_items[indexPath.row]) // not print anything
    }

}

Upvotes: 3

Views: 1654

Answers (2)

JMFR
JMFR

Reputation: 809

The "method signature" (function name) that you are using for the didSelect method is incorrect. What you have listed in your current implementation is the "Swift 2 Version". You might note the way that the argument labels are separated to make the functions a bit more readable like in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)

This same renaming style should be applied to didSelectRowAtIndexPath. Here is the full code you can use to test this out in a playground.

import UIKit

class RightMenuController: UITableViewController {

    let row_items = ["دسته بندی", "ثبت نام/ورود"]

    override func viewDidLoad() {
        // We must register a cell manually since there is no storyboard
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "LabelCell")
        navigationController?.navigationBar.isHidden = true
        super.viewDidLoad()
        self.tableView.delegate = self
        self.tableView.dataSource = self
    }

    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return row_items.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)
        cell.textLabel?.text = row_items[indexPath.row]
        return cell
    }

    // Old Swift 2 method signature
    //func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(self.row_items[indexPath.row]) // not print anything
    }
}

// Create the ViewController
let right = RightMenuController(style: .plain)

// Ask the Playground to show your ViewController in the Assistant (2 rings - Right side view)
import PlaygroundSupport
PlaygroundPage.current.liveView = right

Upvotes: 0

Nathan Mattes
Nathan Mattes

Reputation: 339

Try this instead of your didSelectRow-method:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print(self.row_items[indexPath.row])
}

Upvotes: 5

Related Questions