将UITableView单元格连接到另一个UITableView
我对我的项目(用Swift书写)感到厌倦 我有一个UITableView,其中有一个字符串,在表中提供了十个类别。 我想做的是,选择其中一个类别,然后打开另一个包含其他类别的TableView。并且,当按下secondTableView时,打开该类别的说明 这是我的代码,第一个UITableView可以使用,但是当触摸时什么都不会发生。 现在我想,例如,当我点击“最重要的地方”时,它会打开另一个桌面视图,例如,帝国大厦、自由女神像,当触摸帝国大厦时,会打开一个描述页面将UITableView单元格连接到另一个UITableView,uitableview,swift,tableview,Uitableview,Swift,Tableview,我对我的项目(用Swift书写)感到厌倦 我有一个UITableView,其中有一个字符串,在表中提供了十个类别。 我想做的是,选择其中一个类别,然后打开另一个包含其他类别的TableView。并且,当按下secondTableView时,打开该类别的说明 这是我的代码,第一个UITableView可以使用,但是当触摸时什么都不会发生。 现在我想,例如,当我点击“最重要的地方”时,它会打开另一个桌面视图,例如,帝国大厦、自由女神像,当触摸帝国大厦时,会打开一个描述页面 import UIKit
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!) {
}
}
我能做什么?在故事板上呢?
非常感谢 到目前为止,您还没有为第二个UITableView在didSelectRowAtIndexPath中执行任何操作,您可以很容易地使用另一个包含第二个UITableView的viewcontroller,然后使用第三个viewcontroller了解详细信息。 但是,如果您仍然希望在同一个viewcontroller中有2个UITableView,请按照以下步骤实现所需的结果
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
如果tableview==self.tableview{
返回self.maincogries.count
}
如果tableView==子类别tableView{
返回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
}
}非常简单,请选择将数组传递到下一个tableview,但您需要稍微更改数据源,您能帮我吗?我需要做什么?我是Xcode编程新手。。。真的谢谢你!想法是创建3个ViewController,两个带有UITableView,一个带有说明,但我不知道如何链接所有内容。使用3个ViewController会很容易,连接起来也很容易,添加一个uinavigationcontroller,使其成为rootViewController,并将segue从第一个添加到第二个,将另一个从第二个添加到第三个视图控制器,在prepareForSeuge方法中,在ViewControllers之间传递值。您能解释一下如何连接它们吗?segue是故事板中连接ViewControllers的一种方法。如何使用它?感谢您的耐心:(vc.catId=selectedCategoryId给了我这个错误:“子类别”没有名为“catId”的成员,你必须在第二个viewcontroller中声明这个,就像我在第一个viewcontroller中声明的那样。否,它应该在第一个viewcontroller中声明b。你必须在第二个viewcontroller中声明catId。哦,好的,对不起。但是我如何在第二个viewcontroller中声明catId?使用v它给我的错误和变量selectedCategoryId=-1一样