我在UITableView中有折叠和展开动画。Tableview有两个部分,第一部分数据在其中折叠和展开。这件事与ios 10完美的工作,但在ios 11部分视图重复或重叠的单元格数据是展开的。
下面是我的代码

//MARK: -Table View delegate Method
    func numberOfSections(in tableView: UITableView) -> Int {
        return read_Localizable("titleHelpSection").components(separatedBy: ",").count
    }
    //MARK: -Table View Datasource Method
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat{
        return 44.0
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        var headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "headerView")
        let arrSection = read_Localizable("titleHelpSection").components(separatedBy: ",")
        if headerView == nil
        {
            headerView = UITableViewHeaderFooterView(reuseIdentifier: "headerView")
            headerView?.contentView.backgroundColor = UIColor.white

            let lblResult = UILabel()
            lblResult.tag = 123456
            lblResult.font = AppCommonSNMediumFont()
            lblResult.textColor = UIColor.black
            lblResult.translatesAutoresizingMaskIntoConstraints = false
            headerView?.contentView.addSubview(lblResult)

            let seperator = UIView()
            seperator.translatesAutoresizingMaskIntoConstraints = false
            seperator.backgroundColor = UIColor.black
            headerView?.contentView.addSubview(seperator)

            headerView?.contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[seperator]|", options: [], metrics: nil, views: ["seperator":seperator]))

            headerView?.contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-[lable]-(>=8)-|", options: [], metrics: nil, views: ["lable":lblResult]))

            headerView?.contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-[lable]-[seperator(1)]|", options: [], metrics: nil, views: ["lable":lblResult,"seperator":seperator]))
        }

        if let lblResult = headerView?.contentView.viewWithTag(123456) as? UILabel
        {
            lblResult.text = arrSection[section]
        }

        return headerView

    }

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

        return UITableViewAutomaticDimension

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

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0
        {
            return (arrHelpData.count)
        }
        else
        {
            return 1
        }
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        if indexPath.section == 0
        {
            var cell = tableView.dequeueReusableCell(withIdentifier: "HelpCell") as? CellHelp;
            if cell == nil {
                cell = CellHelp(style: .default, reuseIdentifier: "HelpCell")
                cell?.selectionStyle = .none
                cell?.txtContain.delegate = self
            }

            if let objModel = arrHelpData.object(at: indexPath.row) as? HelpModel
            {
                cell?.lblTitle.text = objModel.helpTitle
                if objModel.isExpanded == true
                {
                    cell?.txtContain.text = objModel.helpDesc
                }
                else
                {
                    cell?.txtContain.text = ""
                }
                cell?.imgArrow.isHighlighted = !objModel.isExpanded
            }
            return cell!
        }
        else
        {
            var cell = tableView.dequeueReusableCell(withIdentifier: "DefultCell")
            if cell == nil
            {
                cell = UITableViewCell(style: .default, reuseIdentifier: "DefultCell")
                cell?.textLabel?.textColor = color1F87A3()
                cell?.textLabel?.font = AppCommonSNRegularFont()
                cell?.selectionStyle = .none
                cell?.textLabel?.numberOfLines = 0
            }

            cell?.textLabel?.text = read_Localizable("titleSettings")
            return cell!
        }
    }



    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 0 && indexPath.row < (arrHelpData.count)
        {
            if let objModel = arrHelpData.object(at: indexPath.row) as? HelpModel
            {
                if objModel.isExpanded == true
                {
                    objModel.isExpanded = false
                }
                else
                {
                    objModel.isExpanded = true
                }
                tableView.reloadData()
            }
        }
    }

实际视图
ios - iOS 11 UITableViewHeaderFooterView无法正确滚动动画-LMLPHP
重叠在单元格数据上的节
ios - iOS 11 UITableViewHeaderFooterView无法正确滚动动画-LMLPHP

最佳答案

这是一个非常令人沮丧的iOS11问题,关于估计的8个问题,如果你真的想保持自己大小的行和标题,那么你需要用下面的方法。
声明变量,该变量保存单元格/标题的高度并将高度存储到其中,并按如下方式使用:

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

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

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
}

这是唯一能解决iOS11自动调整单元格大小问题的解决方案。否则,人们建议保持估计8 0来摆脱这些问题。
在您的情况下,首先尝试这样做的细胞,这并不能完全解决问题,然后做同样的头部高度也。希望这有帮助!
不要忘记在iOS11.1和iOS11.2中进行测试。

关于ios - iOS 11 UITableViewHeaderFooterView无法正确滚动动画,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47925164/

10-11 14:50