Swift ios 9: mudar a posição do cabeçalho da seção após recarregar os dados
Eu tenho UITableView simples com muitas seções e linhas. Seções funcionam bem. Mas, às vezes, após recarregar os dados da tabela, a seção muda de posição. Por exemplo, aconteceu quando eu mudo de guia. Qual poderia ser o problema?
Imagem antes das guias alteradas:
Imagem após guias alteradas:
Atualizar: Adicione o código abaixo:
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))
}
}