Swift UITableView didSelectRowAtIndexPath没有被调用 [英] Swift UITableView didSelectRowAtIndexPath not getting called

查看:129
本文介绍了Swift UITableView didSelectRowAtIndexPath没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IOS开发新手,在处理表格上的单元格选择时遇到问题。每当我选择时,该方法都不会在下面调用 - 任何想法为什么?

New to IOS development and am having trouble with handling cell selection on a table. Whenever I select, the method is not getting called below - any idea why?

我的项目结构是:
查看控制器 - >查看 - >表视图

My project structure is: View Controller -> View -> Table View

以下代码演示了方法调用。其他人被称为没有问题!我知道触摸工作正在下拉成功刷新,点击一个单元格确实突出显示。

The below code demonstrates the method calls. The others get called no problem! I know touch is working as pull down successfully refreshes and on clicking a cell it does become highlighted.

import UIKit

class ViewController: UIViewController, UITableViewDelegate
{

   let blah = ["blah1"]

   //How many sections are in the table?
   func numberOfSectionsInTableView(tableView: UITableView) -> Int {
      return 1
   }

   //How many rows? (returns and int)
   func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return blah.count
   }

  //table contents for each cell?
  //Each time this is called it'll return the next row and thus build a table...
  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
      print("Populating each cell of table view!\n")
      tableView.rowHeight = 80.0
      var cell = UITableViewCell()

      var(a) = blah[indexPath.row]
      var image : UIImage = UIImage(named: a)!
      cell.imageView.image = image

      return cell
  }



  //Code Cell Selected
  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
      println("You selected cell #\(indexPath.row)!")

  }


  func tableView(tableView: UITableViewDelegate, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
     print("wananananaanan" )
     println("You deselected cell #\(indexPath.row)!")

  }




  override func viewDidLoad() {
     super.viewDidLoad()

     // Do any additional setup after loading the view, typically from a nib.

  }

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


推荐答案

你必须在你的 ViewController @IBOutlet 设置为 tableView >并设置为委托 dataSource ,您可以看到数据响应<$ c $中的变化c> tableView 。

You have to set an @IBOutlet to the tableView in you ViewController and set as it's delegate and dataSource to you can see the data an respond to changes in the tableView.

这样的事情:

override func viewDidLoad() {
    super.viewDidLoad()

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

并实现 UITableViewDataSource 协议。

或者你也可以在Interface Builder中设置 ViewController 作为它的委托和 dataSource (我认为更容易)并避免在上面的代码中手动设置。由你决定。

Or you can too in the Interface Builder set the ViewController as it's delegate and dataSource (more easy to do I think) and avoid to set manually in code like above. Is up to you.

我希望这对你有帮助。

这篇关于Swift UITableView didSelectRowAtIndexPath没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆