Swift iOS 9:重新加载数据后节头更改位置 [英] Swift ios 9: Section header change position after reload data

查看:87
本文介绍了Swift iOS 9:重新加载数据后节头更改位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多节和行的普通UITableView.部分工作正常.但是有时在重新加载表的数据后,部分会改变位置.例如,当我更改标签时就发生了.可能是什么问题?

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?

更改标签前的图像:

更改标签后的图像:

更新: 在下面添加代码:

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

}

推荐答案

我知道这是一个旧线程,但是取消选中Adjust Scroll View Insets并没有帮助.我遇到了这个问题,并通过两次重载表解决了这个问题.

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.

这篇关于Swift iOS 9:重新加载数据后节头更改位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆