[英]How to reload selected cell in particular section for expand/collapse in tableview Swift
I am doing expand/collapse tableview cells feature in my iOS app.我正在我的 iOS 应用程序中执行展开/折叠 tableview 单元格功能。 I have multiple sections.
我有多个部分。 And each section has multiple cells.
每个部分都有多个单元格。 By default, cell height is 100, once user taps on cell, I am increasing height to 200.
默认情况下,单元格高度为 100,一旦用户点击单元格,我将高度增加到 200。
So, Based on Bool value, I am changing it.所以,基于布尔值,我正在改变它。 But, While scrolling tableview, It is interchanging the expanded/collapse cells in between sections.
但是,在滚动表格视图时,它正在交换部分之间的展开/折叠单元格。 Like if I tap on first section first cell, It is expanding, but after scrolling tableview, Second section first cell also expanding.
就像我点击第一部分的第一个单元格一样,它正在扩展,但是在滚动表格视图之后,第二部分的第一个单元格也在扩展。
My Requirement is, If user tap on particular cell, that cell only should expand/collapse.我的要求是,如果用户点击特定单元格,则该单元格仅应展开/折叠。 User can manually expand and close.
用户可以手动展开和关闭。 User can expand multiple cells.
用户可以展开多个单元格。
So, I have tried to store Indexpath row and Section.因此,我尝试存储 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()
}
Anyone can give better approach than this?任何人都可以提供比这更好的方法吗?
If you store section and row independently in separate arrays, your algorithm will fail.如果您将部分和行独立存储在单独的 arrays 中,您的算法将失败。 The reason is that both are dependent: Think of three expanded cells (row:1, section:1), (row:2, section:1), (row:3, section:2)
原因是两者都是相互依赖的:想想三个展开的单元格 (row:1, section:1), (row:2, section:1), (row:3, section:2)
Now what happens for the cell (row:3, section:1)?现在单元格(行:3,部分:1)会发生什么? The row-array contains the value "3", and the section-array contains value "1", therefore it will be considered as expanded.
row-array 包含值“3”,section-array 包含值“1”,因此将被视为展开。
Therefore, you need to store the index path as a whole - see the sample code:因此,您需要将索引路径作为一个整体存储 - 参见示例代码:
var expanded:[IndexPath] = []
expanded.append(IndexPath(row:1, section:1))
expanded.append(IndexPath(row:2, section:1))
expanded.append(IndexPath(row:3, section:2))
let checkPath = IndexPath(row:3, section:1)
if (expanded.contains(checkPath)) {
print ("is expanded")
} else {
print ("collapsed")
}
So in your button handle, you'll do the following:因此,在您的按钮句柄中,您将执行以下操作:
@IBAction moreButtonTapped(_ sender: Any) {
if(expanded.contains(indexPath)) {
expanded.removeAll { (checkPath) -> Bool in
return checkPath == indexPath
}
} else {
expanded.append(indexPath)
}
entriesTableView.beginUpdates()
entriesTableView.reloadRows(at: [indexPath], with: .none)
entriesTableView.endUpdates()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.