2016-09-22 57 views
0

如何检测是否轻敲表格视图单元格。网站上的指南和教程遵循向表格单元格视图添加按钮的方法 - 原型单元格。对于所有行,此按钮保持不变,当点击时,返回行或单元格的索引。如何检测表格视图单元格是否在xcode 7中轻敲

我想这样做没有按钮。我怎样才能调用

@IBAction 

当一个单元被点击,然后执行一个segue函数。

我应该怎么办?

添加以下代码,但是这并不能显示任何信息

class MoreViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 

let moreOption = [ 
    ("My Profile"), 
    ("Settings"), 
    ("Logout") 
] 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
    self.navigationController!.navigationBar.barTintColor = UIColor(red: (48/255.0), green: (187/255.0), blue: (197/255.0), alpha: 1.0); 
    self.navigationController!.navigationBar.tintColor = UIColor .whiteColor(); 
    self.navigationController!.navigationBar.translucent = false; 
    self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor .whiteColor()] 
} 

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

func logout() { 
    print("Logout tapped"); 
} 


func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell 
    let (celloption) = moreOption[indexPath.row]; 
    cell.textLabel!.text = celloption; 
    cell.textLabel!.textColor = UIColor(red: (74/255.0), green: (74/255.0), blue: (74/255.0), alpha: 1.0); 
    return cell; 
} 


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    let cell = tableView.cellForRowAtIndexPath(indexPath) 
    print(cell); 
} 

}

+1

你只需要使用didSelectRow的UITableViewDelegate方法。 – Sahil

回答

2

你为什么不使用的UITableViewDelegate方法?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 

在这种方法中,你将得到挖掘电池的indexPath这里本身,如果你愿意,你可以打开新的控制器。

+0

我在didSelectRowAt中使用下面的代码 - let cell = tableView.cellForRowAtIndexPath(indexPath) print(cell); - 但这不是打印任何东西 –

+0

这个方法是否被调用?你有没有设置tableView的数据源和委托? cellForRowAtIndexPath用于创建单元格。使用didSelectRowAtIndexPath来获取单元格点击事件。 – PGDev

+0

是的,我添加了问题中的代码 –

0

尝试这个

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
      let selectedItem = items.objectAtIndex(indexPath.row) as String 
      print(selectedItem) 
     } 
0

(如果您是从代码构建它或变量)你是不是添加的tableview出口的代码。 您需要先在的tableview出口添加到您的类,然后设置apropiate委托和数据源为它

例如

//this is connected to your storyboard 
//It may have typing errors it is not tested 
//at this on the top of your class 
@IBOutlet weak var table: UITableView! 

//at this on viewdidLoad or viewDidAppear 
table.delegate = self 
table.datasource = self 
相关问题