【问题标题】:attempt to insert section 3 but there are only 3 sections after the update尝试插入第 3 节,但更新后只有 3 节
【发布时间】:2018-01-05 08:51:46
【问题描述】:

我有 N 个部分的 Tableview,其中修复了 0,1 个部分。永远不会从 tableview 中删除。但从第2节开始到N个节,可以删除或插入。从第 2 部分到 N 部分 -> 每个部分也有行数。

我在第 2 部分有 N 个部分的搜索功能。
这是整个代码
1。这是包含每个部分的行数的计数 dic ["section" : "no of rows"]

self.countDic.updateValue(1, forKey: "0")
self.countDic.updateValue(0, forKey: "1")
for i in 0..<arra1.count {
    self.countDic.updateValue(array.count, forKey: "\(i + 2)")//[index : arraylist.count]
}


2.这是返回部分和行的 Tableview 方法

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return countDic["\(section)"]!
}
func numberOfSections(in tableView: UITableView) -> Int {
    return countDic.count
}


3.这是搜索和表格重新加载代码

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    let newString = NSString(string: textField.text!).replacingCharacters(in: range, with: string)

    if (newString.isEmpty) {
        FilterStr = ""
    } else {
        FilterStr = newString
    }

    let namesBeginningWithLetterPredicate = NSPredicate(format: "(title1 CONTAINS[cd] $letter)")

    let arr : [Today_ActivityModel] = (Today_Activity_Data as NSArray).filtered(using: namesBeginningWithLetterPredicate.withSubstitutionVariables(["letter": FilterStr])) as! [Today_ActivityModel]
    if FilterStr == "" {
        self.countDic.removeAll()

        FilterData.removeAll()
        //self.GetRecordsCityWiseFilter(ArrayList: self.Today_Activity_Data)
    } else {
        if arr.count >= 0 {

            FilterData.removeAll()
            //self.GetRecordsCityWiseFilter(ArrayList: arr )
        } else {

            self.countDic.removeAll()

            FilterData.removeAll()
            //self.GetRecordsCityWiseFilter(ArrayList: self.Today_Activity_Data)
        }
    }
    DispatchQueue.main.async {
        UIView.performWithoutAnimation {

            let indexSet = IndexSet(integersIn: 2..<self.TblView.numberOfSections)
            self.TblView.reloadSections(indexSet, with: .bottom)
        }
    }
    return true
}

在重新加载表格时,我尝试插入第 3 节,但更新后只有 3 个节
谁能帮我解决这个问题?

【问题讨论】:

  • 为什么不使用两个不同的数组来处理所有这些事情,一个用于前 2 部分,第二个用于其他部分
  • 我将为顶部和最后部分采用两个不同的数组。但最后我必须将表从第 2 部分重新加载到 n 个部分。这是主要的。

标签: ios swift uitableview reloaddata


【解决方案1】:

有两种方法可以重新加载数据。首先 - 调用重新加载数据。它将刷新整个表视图并从 DataSource 请求加载数据。

第二:手动添加数据触发加载数据源的必要部分。

所以基本上如果你不想重新加载所有表格,或者想要有漂亮的动画,你需要先添加新的部分。

TblView.insertSections(IndexSet(integer: 1), with: .left)

请注意,您必须先更改数据,然后让表格视图查询新部分的数据。它会自动从数据源加载数据。

更多详情请访问:https://developer.apple.com/documentation/uikit/uitableview/1614892-insertsections

编辑:我注意到您正在尝试重新加载 UIView.animation 中的表。它不是那样工作的。当您告诉表格视图添加/删除/更新其中的数据时,您可以指定所需的动画,或者它是 .none

更新: 代码示例:

tableView.beginUpdates()
let toUpdateIndexSet = IndexSet(integersIn: 1 ..< tableView.numberOfSections)
tableView.reloadSections(toUpdateIndexSet, with: .fade)

if tableView.numberOfSections < self.numberOfSections(in: tableView) {
    let toAddIndexSet = IndexSet(integersIn: tableView.numberOfSections ..< self.numberOfSections(in: tableView))
    tableView.insertSections(toAddIndexSet, with: .right)
}
tableView.endUpdates()

请注意,当您进行更改时,表格视图会尝试从数据源重新加载内容。但是,例如,如果您在更新之前有一个部分,而不是在第一部分添加第二部分和几行,并尝试重新加载第一部分,您将崩溃(数据源有两个部分,但表视图此时只有一个)。 所以在这种情况下,您需要进行批量更新 - beginUpdates,插入新行并添加新部分,endUpdates。 在这种情况下,数据源将仅在 endUpdates() 时触发。

【讨论】:

  • 你能提供一些我的例子吗? ?即前 2 个部分无法重新加载,其他部分将是,并且所有部分都取决于我的搜索结果。
  • 顺便说一句,将搜索字段和任何其他静态内容作为整个表格视图的标题而不是将其作为部分放置是一种很好的做法。您将获得一些不错的 uikit 内置功能,减少您的代码。
【解决方案2】:

首先直到 UITableView 缓存了节数的详细信息,所以直到您实际重新加载 UITableView numberOfSections 仍会产生旧值。

例如,如果您有这样的节数组:

var sections: [Int] = [2, 1]

还有这个方法:

func numberOfSections(in tableView: UITableView) -> Int {
    return sections.count
}

用于提供节中的行数,然后执行此操作:

sections.append(5)
print (tableView.numberOfSections)
tableView.reloadData()
print (tableView.numberOfSections)

您将得到结果 2 和 3,因为 numberOfSections 在表视图重新加载其数据之前不会更新。

其次,由于这种工作方式,您只能重新加载已经存在的部分,而不是您要添加的新部分。相反,您必须计算出要添加/删除的部分,并改用 insertSections 和 deleteSections 方法。

第三,为什么不直接使用:

self.TblView.reloadData()

让一切为你处理。

【讨论】:

  • self.TblView.reloadData() 我不能使用,因为第 0 部分有搜索文本字段,它也会重新加载。我失去了搜索文本字段的文本。您能否举例说明如何插入/删除部分或其他方式重新加载?
  • 嗯,最好的办法是让搜索文本字段支持变量,这样当它重新加载时,它只会从支持变量更新。否则,您需要弄清楚这些部分是如何变化的并使用适当的方法。因此,如果之前您有 2 个部分,而现在您有 3 个来调用 insertSections,并且仅为第 3 个部分设置了索引。如果您有 5 个部分并且下降到 2,那么您调用 deleteSections 并将索引设置为 3、4 和 5。
  • 嗯..这听起来像是更好的解决方案。但是键盘隐藏/显示呢?
  • 隐藏/显示什么键盘?这会导致什么问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-10
  • 2021-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多