【发布时间】:2019-10-30 06:48:40
【问题描述】:
我使用 tableView 和两个不同的 cell.xib 文件,我想在单击 cell1 时显示,然后我应该显示 cell2 数据。
class TableView: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var array1 = ["Click1","Click2"]
var array2 = [[ "one","two","Three"],["Four","Five"]]
var selectedArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "MainCell", bundle: nil) , forCellReuseIdentifier: "MainCell")//This is used to add xib file with identifier
tableView.register(UINib(nibName: "SecondCell", bundle: nil) , forCellReuseIdentifier: "SecondCell")
}
//MARK:DataSource Methods
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array1.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "MainCell") as! MainCell
cell.textLabel?.text = array1[indexPath.row]
return cell
}
//MARK: tableViewDelegate Method
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "SecondCell") as! SecondCell
selectedArray = array2[indexPath.row]
cell.textLabel?.text = selectedArray[indexPath.row]
}
}
告诉我怎么做
【问题讨论】:
-
如果用户按下cell2然后呢?
-
你为什么在 numberOfRowsInSection 中返回 1?
-
决定你是否需要
change data or cell?在didSelectRowAt你不能dequeue a cell。更改数据或单击重新加载tableview并使用boolean检查将dequeue your second cell。 -
当我点击 click2 时,我需要在同一个 tableView 上显示四个、五个。
-
你的帖子太混乱了。你想创建一个带有可折叠/展开部分的表格视图,对吧?参考:medium.com/@legonaftik/…
标签: ios swift uitableview