[英]How to create Dynamic Table View cells With multiple View Controller in swift 2.0?
Hi all. 大家好。 I'm troubling with tableView in swift. 我很快就麻烦了tableView。 Actually i created the table view with two rows (About and Login) in main view controller. 实际上,我在主视图控制器中创建了具有两行 (关于和登录)的表视图。 Problem at initial, when i click the about or Login , then the New View controller is not opened. 最初出现问题,当我单击about或Login时, 未打开 New View控制器。 But, I try second time to another one row, then the first clicked view Controller is opened at this time of Clicking. 但是,我第二次尝试到另一行,然后在单击时打开第一个单击的视图控制器。 so, this cycles shown wrong view controller at every time of clicking. 因此,此循环在每次单击时显示错误的视图控制器。 Please tell me as whats wrong with my Code?? 请告诉我我的代码有什么问题吗? Please refer screenshot given below.Thanks in advance!!! 请参考下面的截图 。谢谢!!!
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()
}
}
Below Screen shown, I clicked About Row. 在显示的屏幕下方,我单击了关于行。 But, the Login ViewController is Opened. 但是,登录ViewController已打开。
Just update didSelectRowAtIndexPath method 只需更新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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.