我坚持我的项目(用 Swift 编写)
我有一个 UITableView,它有一个字符串,它提供了表格中的十个类别。我想做的是选择这些类别中的一个来触摸它,然后打开一个包含其他类别的 secondTableView。并且,当您按下第二个 TableView 时,打开该类别的描述。
这是我的代码,这适用于拥有第一个 UITableView,但是当触摸时没有任何反应。现在我想,例如,当我点击“最重要的地方”时,它会打开另一个 tableView,例如帝国大厦、自由女神像,当触摸帝国大厦时,会打开一个描述页面。
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!) {
}
}
我能做什么?在 Storyboard 中?非常感谢!
到目前为止,您还没有为第二个 UITableView 在 didSelectRowAtIndexPath 中执行任何操作,您可以轻松地使用另一个包含第二个 uitableview 的 View Controller ,然后使用第三个 View Controller 来获取详细信息。但是如果你仍然想在同一个 View Controller 中有 2 个 uitableviews 那么请按照这些步骤来获得所需的结果。
- 添加另一个 TableView 并设置其数据源和委托(delegate)
- 您必须添加一个 if-else 条件来区分委托(delegate)和数据源方法中的 2 个 tablieview,例如numberOfRowsInSection、cellForRowAtIndexPath、didSelectRowAtIndexPath,下面是一个代码示例,如果您在一个 uiviewcontroller 中使用 2 个 uitableview。您还必须为其他委托(delegate)和数据源方法执行此 if-else。
func tableView(tableView: UITableView, numberOfRowsInSection section:Int)->Int {
if tableview == self.tableView {
返回 self.MainCategories.count
else if tableView == subCategoryTableView {
返回 self.SubCategories.count
}
希望对您有所帮助!
Tôi là một lập trình viên xuất sắc, rất giỏi!