大家好。我在swift中遇到了tableView问题。实际上我在主视图控制器中用两行(关于和登录)创建了表格视图。初始问题,当我点击约或登录,然后新的视图控制器没有打开。但是,我第二次尝试另一行,然后第一次点击视图控制器打开在此单击时。因此,此循环在每次单击时都显示错误的视图控制器。请告诉我,我的代码有什么问题?请参考下面给出的屏幕截图。提前谢谢!!!
import UIKit
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet var tableView: UITableView!
var titles = ["About","LogIn"]
override func viewDidLoad() {
super.viewDidLoad()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titles.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath) as! TableCell
cell.label.text = titles[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
switch indexPath.row
{
case 0: self.performSegueWithIdentifier("aboutSegue", sender: self)
case 1: self.performSegueWithIdentifier("loginSegue", sender: self)
default: break
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "aboutSegue"
{
let vc = segue.destinationViewController as! about
vc.title = "About"
}
else
{
let vc = segue.destinationViewController as! login
vc.title = "Login"
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
在屏幕下方显示,我点击了关于行。但是,Login ViewController已打开。
答案 0 :(得分:1)
只需更新 didSelectRowAtIndexPath 方法
即可func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
switch indexPath.row
{
case 0:
self.performSegueWithIdentifier("aboutSegue", sender: self)
break;
case 1:
self.performSegueWithIdentifier("loginSegue", sender: self)
break;
default: break
}
}