【发布时间】:2020-03-03 08:00:37
【问题描述】:
我是 xcode 和 swift 的新手,我找到了在按下整个单元格时展开单元格的指南。 现在我想在按下单元格内的按钮时展开单元格。
我的视图控制器中有这个:
//datasource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView.tag == 100 {
return nameArr.count
}else{
return prova.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView.tag == 100 {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell") as! MainTableViewCell
cell.lblName.text = nameArr[indexPath.row]
cell.expand.tag = indexPath.row
return cell
}else{
tableView.estimatedRowHeight = 60
tableView.rowHeight = UITableView.automaticDimension
let cell = tableView.dequeueReusableCell(withIdentifier: "InsideTableViewCell") as! InsideTableViewCell
cell.lblInsideName.text = prova[indexPath.row]
return cell
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//se la cella è selezionata e deve essere tirata ancora giù ritorna 243 che sarebbe il valore della cella + l'immagine dentro da mostrare
if selectedIndex == indexPath.row && isCollapsed == true && tableView.tag == 100 {
return 375
}else {
//altrimenti se è gia collassata e ripremiamo sulla cella ritorna 50 e quindi richiude la cella
return 96
}
}
//delegate
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if selectedIndex == indexPath.row {
if self.isCollapsed == false {
self.isCollapsed = true
}else{
self.isCollapsed = false
}
}else{
self.isCollapsed = true
}
self.selectedIndex = indexPath.row
//tableView.reloadRows(at: [indexPath], with: .automatic)
tableView.beginUpdates()
tableView.endUpdates()
}
但我不知道该怎么做。
在我的 viewController 现在我有这个变量:
var selectedIndex = -1 //tell me which cell i pressed
var isCollapsed = false // tell if the cell is already collapsed
【问题讨论】:
-
你想从单元格按钮切换你的
isCollapsed吗? -
是的,当按下单元格内的按钮时,我想折叠单元格
-
stackoverflow.com/questions/60226353/… ,可以在cellForRowAt中获取cell的buttonclick。检查这个。 & 将增加单元格高度的逻辑应用到回调闭包中(在 cellForRowAt 方法中)。
-
这是所有表格单元格还是只是特定单元格?