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

Abh*_*tra 3 uitableview ios swift uitableviewrowaction ios11

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

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

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

适用于iOS 11.2的GIF图像

在此输入图像描述

适用于iOS 11.1的GIF图像

在此输入图像描述

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

任何帮助将不胜感激.

谢谢

小智 8

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

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

var cellHeightDictionary: NSMutableDictionary // To overcome the issue of iOS11.2
Run Code Online (Sandbox Code Playgroud)

在viewDidLoad中初始化,

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 125
    cellHeightDictionary = NSMutableDictionary()
}
Run Code Online (Sandbox Code Playgroud)

并使用如下:

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
}
Run Code Online (Sandbox Code Playgroud)

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


Abh*_*tra 4

**适用于 ios 11.2。* **

tableView.rowHeight = UITableViewAutomaticDimension 中viewDidLoad()

添加estimatedHeightForRowAt

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

        if indexPath.row == 0{

            return firstCellHeight()
        }
        else{

            return UITableViewAutomaticDimension
        }
}
Run Code Online (Sandbox Code Playgroud)

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
    }
}
Run Code Online (Sandbox Code Playgroud)

并且不要使用:reloadRows

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

而是使用reloadDataietableView.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)
        }
Run Code Online (Sandbox Code Playgroud)