UITableView Reload Rows在iOS 11.2中不自然地颠覆了TableView

问题描述 投票:3回答:4

通过调用更新iOS 11.2的新XCODE 9.2后出现了奇怪的问题

let indexPath = IndexPath(item: 0, section: 1)
self.tableViewHome.reloadRows(at: [indexPath], with: .none)

它导致整个TableView不必要的抖动,事先对这些代码非常好。

适用于iOS 11.2的GIF图像

enter image description here

适用于iOS 11.1的GIF图像

enter image description here

如何阻止这个Jerking,我必须在两个单元格中重新加载tableview中的一个单元格。

任何帮助将不胜感激。

谢谢

ios swift uitableview ios11 uitableviewrowaction
4个回答
8
投票

我的理解是,如果你真的想要使用自动调整大小(并且所有单元格大小不同),那么上述解决方案对iOS 11.2不起作用,所以我尝试的如下:

声明变量以存储单元格的高度

var cellHeightDictionary: NSMutableDictionary // To overcome the issue of iOS11.2

在viewDidLoad中初始化,

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 125
    cellHeightDictionary = NSMutableDictionary()
}

并使用如下:

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cellHeightDictionary.setObject(cell.frame.size.height, forKey: indexPath as NSCopying)
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    if cellHeightDictionary.object(forKey: indexPath) != nil {
        let height = cellHeightDictionary.object(forKey: indexPath) as! CGFloat
        return height
    }
    return UITableViewAutomaticDimension
}

这对我有用,希望这对你也有帮助


4
投票

最后我得到了答案,我必须使用UITableView的委托方法,即

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {

            return UITableViewAutomaticDimension
    }

通过返回UITableViewAutomaticDimension放置此方法后,它停止了对iOS 11.2的猛烈攻击。


4
投票

**对于ios 11.2。* **

tableView.rowHeight = viewDidLoad()中的UITableViewAutomaticDimension

添加estimatedHeightForRowAt

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {

        if indexPath.row == 0{

            return firstCellHeight()
        }
        else{

            return UITableViewAutomaticDimension
        }
}

firstCellHeight是我的第一个细胞高度

func firstCellHeight() - > CGFloat {

    if UIDevice.Display.typeIsLike == UIDevice.DisplayType.ipad{

        return 400

    }else if UIDevice.Display.typeIsLike == UIDevice.DisplayType.iphone7plus{

        return 230

    }else if UIDevice.Display.typeIsLike == UIDevice.DisplayType.iphoneX{

        return 200

    }else{

        return 180
    }
}

并且不要使用:reloadRows

    let indexPath = IndexPath(item: 1, section: 0)
    tableView.reloadRows(at: [indexPath], with: .none)

而是使用reloadData,即tableView.reloadData()

最后,如果您的行动需要,请使用以下代码:

let indexPath0 = IndexPath(item: 0, section: 0)
        if !((tableView.indexPathsForVisibleRows?.contains(indexPath0))!) {
            print("Not Visible")

            self.tableView.setContentOffset(CGPoint(x:0, y:firstCellHeight() ), animated: false)
        }

3
投票

我有同样的问题。我做错了一件事。选择了故事板中的自动行高和自动估计高度,并且在代码中也实现了heightForRowAt方法。我取消选中自动行高,只实现了heightForRowAt方法。它解决了我的问题。

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
    return 390
}

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.