2017-02-14 3 views
1

コントローラの各セルをクリックして処理したいのですが、ログに何も表示されません!UITableViewControllerのハンドルセルのクリック方法

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 
    } 

} 

答えて

2

代わりにdidSelectRow-方法、これを試してください:あなたはdidSelect方法で使用している

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

"メソッドのシグネチャ"(関数名)が間違っています。現在の実装であなたが挙げているのは、 "Swift 2 Version"です。関数をより読みやすくするために引数のラベルを区切る方法に注意してください。tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)

didSelectRowAtIndexPathに同じ名前変更スタイルを適用する必要があります。ここでは、これをプレイグラウンドでテストするために使用できる完全なコードを示します。

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 
関連する問題