Ios 需要按字母顺序对UITableView进行分组,并具有展开/折叠功能
我需要实现一个Ios 需要按字母顺序对UITableView进行分组,并具有展开/折叠功能,ios,swift,uitableview,Ios,Swift,Uitableview,我需要实现一个UITableView,它的字母顺序与联系人应用程序中的相同,然后在选择任何单元格时,它都应该展开。下面是我的字母顺序代码 var houseHoldArray = ["Ahouse", "AAhouse", "Bhouse", "BBhouse", "BBBhouse", "CHouse", "CCHouse", "CCHouse", "DHouse", "EHouse", "HHouse", "XHouse", "ZHouse"] var houseHoldAl
UITableView
,它的字母顺序与联系人应用程序中的相同,然后在选择任何单元格时,它都应该展开。下面是我的字母顺序代码
var houseHoldArray = ["Ahouse", "AAhouse", "Bhouse", "BBhouse", "BBBhouse", "CHouse", "CCHouse", "CCHouse", "DHouse", "EHouse", "HHouse", "XHouse", "ZHouse"]
var houseHoldAlphabet = ["A", "B", "C", "D", "E", "H", "X", "Z"]
var houseHoldArrayOfDic = [
"A": ["Ahouse", "AAhouse"],
"B": ["Bhouse", "BBhouse", "BBBhouse"],
"C": ["CHouse", "CCHouse", "CCHouse"],
"D": ["DHouse"],
"E": ["EHouse"],
"H": ["HHouse"],
"X": ["XHouse"],
"Z": ["ZHouse"]
]
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return houseHoldArrayOfDic[houseHoldAlphabet[section]]!.count
}
public func numberOfSections(in tableView: UITableView) -> Int {
return houseHoldAlphabet.count
}
public func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return houseHoldAlphabet[section]
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
}
cell?.textLabel?.text = houseHoldArrayOfDic[houseHoldAlphabet[indexPath.section]]?[indexPath.row]
// Configure the cell...
return cell!
}
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return houseHoldAlphabet
}
func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
return houseHoldAlphabet.index(of: title)!
}
现在假设如果我选择了一个house,那么它应该被展开并显示子单元。每一行都应该是可扩展的
我知道在UITableView上应用展开和折叠的两种方法
需要建议 您是否尝试过使用选项2,使用
tableView.beginUpdates()
后跟tableView.endUpdates()
而不是tableView.reloadData
?这将在不重新加载整个数据源的情况下触发高度更改您是否尝试使用选项2使用tableView.beginUpdates()
后跟tableView.endUpdates()
而不是tableView.reloadData
?这将在不重新加载整个数据源的情况下触发高度更改查看此示例项目:这可能会帮助您:查看此示例项目:这可能会帮助您:和