Eduard Kornev
Eduard Kornev

Reputation: 584

Swift ios 9: Section header change position after reload data

I have plain UITableView with many sections and rows. Sections work fine. But sometimes after reload data of table, section change position. For example it was happened when i change tabs. What could be the problem?

Image before changed tabs:

Before change tab

Image after changed tabs:

After change tab

Update: Add code below:

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))
    }

}

Upvotes: 13

Views: 9521

Answers (7)

alokard
alokard

Reputation: 79

It looks like the issue in estimated height for your table view cells. Due to internal optimisations system calculates the height of absent cells based on estimated height value, that's why it can position your headers on wrong calculated height (like real cells bigger or smaller than their estimated size). It usually happens when you call reloadData() in the middle of the list. As a proper solution you should avoid calling reloadData() when you want to update the content of the list with second page for example. So you should use tableView.beginUpdates() and tableView.endUpdates() and update items collection for table view. There is also new closure based API performBatchUpdates (just like it was for UICollectionView), but it's available from iOS 11.

Hope it may help to find proper solution for you

Upvotes: 1

Haoxin Li
Haoxin Li

Reputation: 81

Calling reloadData() twice also works for me, but I would suggest calling tableView.layoutSubviews() after reloadData(), which also works for me.

Upvotes: 3

Leonardo
Leonardo

Reputation: 1750

The solutions here didn't fix my problem, so I'm leaving one more possible solution that helped me.

Set the estimatedSectionHeaderHeight, it seems that my tableView wasn't being able to calculate the initial position of my first section without it.

Upvotes: 0

guozqzzu
guozqzzu

Reputation: 899

In my project ,this error happened when i pull up tableView to request more data then reload the data after requesting. I need create many sections after requesting the data and it may effect the performance if i reloadData twice. More than, i found that if i scroll up or down the tableView, the position of section's headerView get right. So i fix this bug(my condition) i requesting the data by this:

[_tableView reloadData];
CGPoint offSetOrigin     = _tableView.contentOffset;
CGPoint offSetSecond     = CGPointMake(0, offSet.y-1);
_tableView.contentOffset = offSetSecond;
_tableView.contentOffset = offSet;

Upvotes: 0

user3480295
user3480295

Reputation: 1048

tableView.reloadData()
tableView.layoutIfNeeded()
tableView.beginUpdates()
tableView.endUpdates()

This help me out.

Double reloadData will cause flickering while this solution won't.

Upvotes: 22

Hebbian
Hebbian

Reputation: 344

I know it's an old thread but uncheck the Adjust Scroll View Insets doesn't help. I had this problem and solved it by reload table twice.

Upvotes: 7

Sucharu Hasija
Sucharu Hasija

Reputation: 1126

This is because of UI viewcontroller property. Please first select UIViewController From Storyboard , Attribute Inspector

enter image description here

Upvotes: 0

Related Questions