Swift ios 9:重新加载数据后节头更改位置
我有很多节和行的纯UITableView。分区工作正常。但有时在重新加载表的数据后,节会改变位置。例如,它发生在我更改选项卡时。有什么问题吗 更改选项卡之前的图像: 更改选项卡后的图像: 更新: 在下面添加代码:Swift ios 9:重新加载数据后节头更改位置,swift,uitableview,ios9,Swift,Uitableview,Ios9,我有很多节和行的纯UITableView。分区工作正常。但有时在重新加载表的数据后,节会改变位置。例如,它发生在我更改选项卡时。有什么问题吗 更改选项卡之前的图像: 更改选项卡后的图像: 更新: 在下面添加代码: override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) self.dishesListTableView.reloadData() } override func
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.dishesListTableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func customizeCollectionView() {
self.collectionView.delegate = self
self.collectionView.dataSource = self
self.collectionView.dataProviderDelegate = self.dishesListDataProvider
self.collectionView.showsHorizontalScrollIndicator = false
self.collectionView.decelerationRate = UIScrollViewDecelerationRateFast
self.collectionView.registerClass(HPCollectionViewCell.self, forCellWithReuseIdentifier: HPCollectionViewCellConstants.reuseIdentifier)
}
//MARK: - UITableViewDelegate
func customziseDishesListTableView(){
self.dishesListTableView.delegate = self
self.dishesListTableView.dataSource = self
self.dishesListTableView.registerNib(UINib(nibName: DishesListSingleTableViewCell.nibName, bundle: nil), forCellReuseIdentifier: DishesListSingleTableViewCell.nibName)
self.dishesListTableView.registerNib(UINib(nibName: DishesListSinglePizzaTableViewCell.nibName, bundle: nil), forCellReuseIdentifier: DishesListSinglePizzaTableViewCell.nibName)
self.dishesListTableView.estimatedRowHeight = 123
self.dishesListTableView.rowHeight = UITableViewAutomaticDimension
if self.dishesListDataProvider.isPizza() {
self.dishesListTableView.separatorColor = UIColor.clearColor()
}else{
self.dishesListTableView.separatorColor = UIColor.expSilverColor()
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dishesListDataProvider.countOfDishes(section)
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.dishesListDataProvider.countOfKitchenTypes()
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if self.dishesListDataProvider.getDishItemByIndex(indexPath.section, indexDish: indexPath.row).isPizza() {
let cell = tableView.dequeueReusableCellWithIdentifier(DishesListSinglePizzaTableViewCell.nibName, forIndexPath: indexPath) as! DishesListSinglePizzaTableViewCell
cell.customizeView(self.dishesListDataProvider.getDishItemByIndex(indexPath.section, indexDish: indexPath.row), dishesListSingleTableViewCellProtocol: self, indexPath: indexPath)
return cell
}else{
let cell = tableView.dequeueReusableCellWithIdentifier(DishesListSingleTableViewCell.nibName, forIndexPath: indexPath) as! DishesListSingleTableViewCell
cell.customizeView(self.dishesListDataProvider.getDishItemByIndex(indexPath.section, indexDish: indexPath.row), dishesListSingleTableViewCellProtocol: self, indexPath: indexPath)
return cell
}
}
func tableView(tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
self.collectionView.scrollToIndex(section, animated: true)
self.collectionView.changeSelectionForCellAtIndexPath(NSIndexPath(forItem: section, inSection: 0))
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionView = NSBundle.mainBundle().loadNibNamed(DishesListSectionView.nibName, owner: self, options: nil)[0] as! DishesListSectionView
sectionView.customizeView(self.dishesListDataProvider.getKitchenItemByIndex(section).kitchenTypeTitle)
return sectionView
}
//MARK: - UIScrollViewDelegate
func scrollViewDidScroll(scrollView: UIScrollView) {
if currentSectionIndex != self.dishesListTableView.indexPathsForVisibleRows![0].section {
currentSectionIndex = self.dishesListTableView.indexPathsForVisibleRows![0].section
self.collectionView.scrollToIndex(currentSectionIndex, animated: true)
self.collectionView.changeSelectionForCellAtIndexPath(NSIndexPath(forItem: currentSectionIndex, inSection: 0))
}
}
这是因为UI viewcontroller属性。请首先从情节提要、属性检查器中选择UIViewController
我知道这是一个旧线程,但取消选中
调整滚动视图插图
没有帮助。我遇到了这个问题,并通过两次重新加载表解决了这个问题。在我的项目中,当我打开tableView
请求更多数据,然后在请求后重新加载数据时,发生了这个错误。我需要在请求数据后创建许多节,如果我两次reloadData
,可能会影响性能。不仅如此,我还发现,如果我向上或向下滚动表视图
,节头视图的位置就会正确。因此,我修复了这个bug(我的情况),我通过以下方式请求数据:
[_tableView reloadData];
CGPoint offSetOrigin = _tableView.contentOffset;
CGPoint offSetSecond = CGPointMake(0, offSet.y-1);
_tableView.contentOffset = offSetSecond;
_tableView.contentOffset = offSet;
这帮了我的忙
双击
reloadData
将导致闪烁,而此解决方案不会 这里的解决方案没有解决我的问题,所以我留下一个可能的解决方案来帮助我
设置,如果没有它,我的tableView似乎无法计算第一节的初始位置。调用reloadData()两次对我也有效,但我建议在reloadData()之后调用tableView.layoutSubviews(),这也适用于我。这看起来像是表格视图单元格的估计高度问题。由于内部优化,系统根据估计的高度值计算缺少单元格的高度,这就是为什么它会将标题放置在错误的计算高度上(如实际单元格大于或小于其估计大小)。当您在列表中间调用<代码> RealDeDATA()/Cuff>时,通常会发生这种情况。作为一个合适的解决方案,当您想用第二页更新列表内容时,应该避免调用
reloadData()
。因此,您应该使用tableView.beginUpdate()
和
tableView.endUpdates()
和表视图的更新项集合。还有新的基于闭包的APIPerformBatChUpdate
(与UICollectionView
一样),但它可以从iOS 11上获得
希望它能帮助你找到合适的解决方案照你说的做。对我没有帮助您是否也更改了主选项卡栏控制器?请检查此项。那么在滚动视图中可能会出现此问题,谢谢。Double reloadData()帮助我。@EduardKornev Double reloadData()调用是一个奇怪的解决方案。“有谁知道更好的解决办法吗?”罗曼波季莫夫在下面又给出了一个答案,这对一些人有所帮助。你们可以试试。@EduardKornev,你们现在用的是什么解决方案?@RomanPodymov,我用的是双重加载的解决方案。另一个解决方案对我没有帮助。极好的答案。谢谢,我在iOS 8和iOS 9上调用UITableViewAutomaticDimension节页脚高度的BeginUpdate/EndUpdate时崩溃。将tableview的估计高度更改为至少与最大单元格高度相同,并使用tableview.BeginUpdate()和tableview.EndUpdate解决了这个问题。
tableView.reloadData()
tableView.layoutIfNeeded()
tableView.beginUpdates()
tableView.endUpdates()