ios - 部分问题的 Swift 可扩展表视图标题
问题描述
我对按标题的可扩展表格视图有疑问。我为第 0 部分制作了它并且它工作正常,但是当我尝试为第 1 部分制作相同的东西时,它会给出错误,即
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 0 节中的行数无效。更新 (0) 后现有节中包含的行数必须等于该节中包含的行数更新前的节 (2),加上或减去从该节插入或删除的行数(0 插入,0 删除),加上或减去移入或移出该节的行数(0 移入,0 移动出去)。
如果我没有触发第二个标题功能,第一个仍然有效。当我单击第二个标题(这是第一节)时,它给出了该错误。
这是我的标题视图代码:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
let button = UIButton(type: .system)
button.setTitle("Kapat", for: .normal)
button.setTitleColor(.black, for: .normal)
button.backgroundColor = .lightGray
button.titleLabel!.font = UIFont.boldSystemFont(ofSize: 14)
button.addTarget(self, action: #selector(handleExpandCloseForAlim(sender:)), for: .touchUpInside)
return button
}
if section == 1 {
let button2 = UIButton(type: .system)
button2.setTitle("Kapat", for: .normal)
button2.setTitleColor(.black, for: .normal)
button2.backgroundColor = .lightGray
button2.titleLabel!.font = UIFont.boldSystemFont(ofSize: 14)
button2.addTarget(self, action: #selector(handleExpandCloseForTeslim(sender:)), for: .touchUpInside)
return button2
}
else{
return nil
}
}
这是我的动作功能:
@objc func handleExpandCloseForAlim(sender: UIButton) {
var indexPaths = [IndexPath]()
for row in self.userAdressDict.indices{
let indexPath = IndexPath(row: row, section: 0)
indexPaths.append(indexPath)
}
let indexPath = IndexPath(row: (sender.tag), section: 0)
let adresso = self.userAdressDict[indexPath.row]
let isExp = adresso.isExpanded
adresso.isExpanded = !isExp!
if isExp! {
tableView.deleteRows(at: indexPaths, with: .fade)
}else {
tableView.insertRows(at: indexPaths, with: .fade)
}
print(adresso.isExpanded!, adresso.userMahalle!)
}
@objc func handleExpandCloseForTeslim(sender: UIButton) {
var indexPathsForTeslim = [IndexPath]()
for row in self.userAdressDictForTeslim.indices{
let indexPath = IndexPath(row: row, section: 1)
indexPathsForTeslim.append(indexPath)
}
let indexPath = IndexPath(row: (sender.tag), section: 1)
let adresso = self.userAdressDictForTeslim[indexPath.row]
let isExp = adresso.isExpanded
adresso.isExpanded = !isExp!
if isExp! {
tableView.deleteRows(at: indexPathsForTeslim, with: .fade)
}else {
tableView.insertRows(at: indexPathsForTeslim, with: .fade)
}
print(adresso.isExpanded!, adresso.userMahalle!)
}
这里是我的 numberOfRowsInSection 部分:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
guard let element = self.userAdressDict.first as? adresStruct else { return 0 }
if element.isExpanded {
return self.userAdressDict.count
}
else {
return 0
}
}
if section == 1 {
guard let teslim = self.userAdressDictForTeslim.last as? adresStruct else { return 0 }
if teslim.isExpanded {
return self.userAdressDictForTeslim.count
}
else {
return 0
}
}
return 1
}
我找不到任何解决方案。我认为发件人的东西工作不正常,但这可能是另一个问题。我希望我问清楚。请帮助我。
解决方案
我没有深入研究您的代码,但我将其复制粘贴到 Xcode 项目中进行研究。所以有基本的方法可以解决您的问题,希望对您有所帮助。
我想你的课是这样的。
class Adres{
var title: String = ""
var isExpanded: Bool = true
init(title: String, isExpanded: Bool){
self.title = title
self.isExpanded = isExpanded
}
}
其中主要有两个不同的变量adresDict
& adresDictForTeslim
。
所以我保留了一个锁数组来做可扩展的逻辑东西。
var keys: [Int] = [1,1]
<1> Show
<0> Hide
现在,数组元素显示在 UITableView 中,因为它已扩展。
有模拟数据。
var userAdressDict = [Adres(title: "First Adres", isExpanded: true) ,Adres(title: "Second Adres", isExpanded: true)]
var userAdressDictForTeslim = [Adres(title: "First Teslim", isExpanded: true),Adres(title: "Second Teslim", isExpanded: true)]
viewForHeaderInSection
也是一样的。
并numberOfRowsInSection
检查数组是否扩展了键。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
if self.keys[0] == 1 {
return userAdressDict.count
}else{
return 0
}
}else{
if self.keys[1] == 1 {
return userAdressDictForTeslim.count
}else{
return 0
}
}
}
然后在处理函数中检查这些键。
@objc func handleExpandCloseForAlim(sender: UIButton) {
if keys[0] == 0 {
keys[0] = 1
}else{
keys[0] = 0
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
如果表格视图中的第一个数组元素被展开,则关闭它,或者不展开它。
然后在另一个函数中。
@objc func handleExpandCloseForTeslim(sender: UIButton) {
if keys[1] == 0 {
keys[1] = 1
}else{
keys[1] = 0
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
到现在为止还挺好。它正在我的模拟器上运行。
然后有更有效的方法来处理扩展键。
将您UIButton's
的 sectionHeader 作为您的公共变量ViewController
并控制它,如果它的标题是"Kapat"
并单击它,那么它必须是"Aç"
,或者如果它"Aç"
并且触摸它必须是"Kapat"
。
推荐阅读
- php - 如何在php中转换数组字符串
- java - 无法从 AWS Lambda 连接到 AWS RDS MySql DB。ClassNotFoundException:com.mysql.jdbc
- python - Python:迭代 API 字节字符串
- c# - 为什么我在这里得到“无法将 null 转换为 'ScheduleType',因为它是不可为空的值类型”?
- docker - 使用 gcsfuse-mounted Bucket 中的数据在 Google Cloud 实例上运行 Docker
- r - 构建“R Packages”一书时Windows系统调用失败
- python - 如何从可训练的张量流变量转换为不可训练的张量流变量?
- java - Java junit 断言已引发异常
- stm32 - 关于 FreeRTOS 如何在 STM32 平台上排队任务的一些困惑
- ios - 为什么 armv7s / arm64e 架构不增加封装大小?