我的项目(用Swift编写)
我有一个UITableView,它有一个字符串,在表中提供了十个类别。我想做的是,从这些类别中选择一个接触它的类别,并打开一个包含其他类别的secondTableView。然后,当您按下第二个TableView时,打开该类别的描述。
这是我的代码,它可以有第一个UITableView,但当触摸时什么都不会发生。现在,例如,当我单击"最重要的地方"时,它会打开另一个表View,例如,Empire State Building,Statue of Liberty,当触摸Empire State大厦时,会打开一个Description页面。
import UIKit
class Categories: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView: UITableView!
var MainCategories: [String] = ["Most important places", "Monuments", "Nature", "Churches", "Museums", "Streets", "Zones", "Weather", "Events", "Favourites"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.MainCategories.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = self.MainCategories[indexPath.row]
return cell
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
}
}
我能做什么?故事板里呢?非常感谢!
到目前为止,您还没有在didSelectRowAtIndexPath中为第二个UITableView做任何操作,您可以很容易地使用另一个视图控制器,它包含第二个UITableView,然后是第三个视图控制器以获取详细信息。但如果你仍然想在同一个视图控制器中有两个合适的视图,那么请按照以下步骤来获得所需的结果。
- 添加另一个表视图并设置其数据源和委托
- 您必须添加一个if-else条件来区分委托和数据源方法中的两个tablelieview,例如numberOfRowsInSection、cellForRowAtIndexPath、didSelectRowAtIndexPath。如果您在一个uiview控制器中使用两个uitableview,下面是代码示例。如果对其他委托和数据源mehto也是如此,则必须执行此操作
func tableView(tableView: UITableView, numberOfRowsInSection section:Int)->Int {
if tableview == self.tableView {
return self.MainCategories.count
}
else if tableView == subCategoryTableView {
return self.SubCategories.count
}
}
希望它能有所帮助!
我已经更改了您的代码,希望这能帮助
import UIKit
class Categories: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView: UITableView!
**var selectedCategoryId = -1**
var MainCategories: [String] = ["Most important places", "Monuments", "Nature", "Churches", "Museums", "Streets", "Zones", "Weather", "Events", "Favourites"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.MainCategories.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = self.MainCategories[indexPath.row]
return cell
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
**selectedCategoryId = indexPath.row**
}
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
let vc = segue.destinationViewController as YOUR_SECOND_VIEW_CONTROLLER
vc.catId = selectedCategoryId
}
}