【发布时间】:2021-05-12 02:36:04
【问题描述】:
我正在我的 iOS 应用程序中进行展开/折叠表格视图单元格功能。我有多个部分。每个部分都有多个单元格。默认情况下,单元格高度为 100,一旦用户点击单元格,我将高度增加到 200。
所以,基于 Bool 值,我正在更改它。但是,在滚动表格视图时,它正在交换部分之间的展开/折叠单元格。 就像我点击第一部分的第一个单元格一样,它正在扩展,但在滚动 tableview 之后,第二部分的第一个单元格也在扩展。
我的要求是,如果用户点击特定单元格,则该单元格仅应展开/折叠。用户可以手动展开和关闭。用户可以展开多个单元格。
所以,我尝试存储 Indexpath 行和部分。
var expandedIndexSet : IndexSet = []
var expandedIndexSection : IndexSet = []
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:"cellIdentifier", for:
indexPath) as! MyTableViewCell
if expandedIndexSet.contains(indexPath.row) && expandedIndexSection.contains(indexPath.section) { // expanded true
cell.height = 200
//some other data loading here
}
else { //expanded false
cell.height = 100
}
}
@IBAction moreButtonTapped(_ sender: Any) {
if(expandedIndexSet.contains(indexPath.row)) && expandedIndexSection.contains(indexPath.section){
expandedIndexSet.remove(indexPath.row)
expandedIndexSection.remove(indexPath.section)
} else {
expandedIndexSet.insert(indexPath.row)
expandedIndexSection.insert(indexPath.section)
}
entriesTableView.beginUpdates()
entriesTableView.reloadRows(at: [indexPath], with: .none)
entriesTableView.endUpdates()
}
谁能给出比这更好的方法?
【问题讨论】:
标签: ios swift iphone uitableview expandablelistview