Ios 尝试插入第3节,但更新后只有3节
我有N个分区的Tableview,其中0,1个分区是固定的。它永远不会从tableview中删除。但从第2节开始到N节数,可以删除或插入。从第二节到第N节->每个节也有行数。Ios 尝试插入第3节,但更新后只有3节,ios,swift,uitableview,reloaddata,Ios,Swift,Uitableview,Reloaddata,我有N个分区的Tableview,其中0,1个分区是固定的。它永远不会从tableview中删除。但从第2节开始到N节数,可以删除或插入。从第二节到第N节->每个节也有行数。 我在第二部分有搜索功能,可以搜索N个部分这是全部代码 1。这是包含每个节的行数的计数dic[“节”:“行数”] self.countDic.updateValue(1, forKey: "0") self.countDic.updateValue(0, forKey: "1") for i in 0..<arra1.
我在第二部分有搜索功能,可以搜索N个部分
这是全部代码
1。这是包含每个节的行数的计数dic[“节”:“行数”]
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]
}
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
}
func textField(textField:UITextField,shouldChangeCharacters范围:NSRange,replacementString:string)->Bool{
让newString=NSString(string:textField.text!)。replacingCharacters(in:range,with:string)
if(newString.isEmpty){
FilterStr=“”
}否则{
FilterStr=newString
}
让namesbeginingwithletterpredicate=NSPredicate(格式:“(标题1包含[cd]$letter)”)
让arr:[Today\u ActivityModel]=(Today\u Activity\u数据作为NSArray)。筛选(使用:namesBeginingWithLetterPredicate。使用替换变量([“letter”:FilterStr]))作为![Today\u ActivityModel]
如果过滤器TR==“”{
self.countDic.removeAll()
FilterData.removeAll()
//self.GetRecordsCityWiseFilter(ArrayList:self.Today\u活动\u数据)
}否则{
如果arr.count>=0{
FilterData.removeAll()
//self.GetRecordsCityWiseFilter(ArrayList:arr)
}否则{
self.countDic.removeAll()
FilterData.removeAll()
//self.GetRecordsCityWiseFilter(ArrayList:self.Today\u活动\u数据)
}
}
DispatchQueue.main.async{
UIView.Perform不带动画{
首先让indexSet=indexSet(integersIn: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()
有两种方法可以重新加载数据。首先,调用reload data。它将刷新整个表视图,并从数据源请求加载数据
第二:在数据源的必要部分手动添加触发加载的数据
所以基本上,若你们不想重新加载所有的表,或者想要有好的动画,你们需要先添加新的部分
TblView.insertSections(IndexSet(integer: 1), with: .left)
请注意,您必须先更改数据,然后让表视图查询新节的数据。它将自动从数据源加载数据
有关更多详细信息,请访问:
编辑:我注意到您正在尝试在UIView.animation中重新加载表。它不是这样工作的。当您告诉table view在其中添加/删除/更新数据时,可以指定所需的动画,也可以指定为.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()
tableView.beginUpdate()
让toUpdateIndexSet=IndexSet(整数:1..
请注意,当您进行更改时,表视图会尝试从数据源重新加载内容。但如果您在更新之前有一个节,然后在第一节中添加第二节和几行,并尝试重新加载第一节,则会发生崩溃(数据源有两个节,但表视图此时只有一个节)。
因此,在这种情况下,您需要进行批更新-beginUpdates,插入新行并添加新节,endUpdates。
在这种情况下,数据源将仅在endUpdates()处触发。self.TblView.reloadData()我不能使用,因为第0部分有搜索文本字段,它也会重新加载。我不喜欢搜索文本字段的文本是什么。你能举例说明如何插入/删除部分或其他重新加载的方法吗?最好是有一个变量作为搜索文本字段的后盾,这样当它重新加载时,它只会从中更新backing变量。否则,您需要计算出节是如何更改的,并使用适当的方法。因此,如果以前有2个节,现在有3个节,则调用insertSections,并为第3个节设置索引集。如果有5个节,则向下调用2个节,则调用deleteSections,并为3、4和5设置索引。嗯..i这听起来是更好的解决方案。但是键盘隐藏/显示呢?什么键盘隐藏/显示?这会导致什么问题?为什么不使用两个不同的数组来处理所有这些事情呢?一个用于前2个部分,另一个用于其他部分。我将使用两个不同的数组来处理顶部和最后一个部分。但是在最后一个部分,我我需要将表格从第2部分重新加载到n个部分。这是最主要的。你能提供一些我的例子吗?例如,前2部分无法重新加载,其他部分将无法重新加载,所有部分都取决于我的搜索结果。顺便提一下,将搜索字段和任何其他静态内容作为整个表格的标题是一个很好的做法视图,而不是将其作为部分放置。您将获得一些漂亮的uiki
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()