HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 添加单元格后TableView Auto Scrolling行为不正常大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
查看设置:

我的TableView有3个部分,每个部分有4或9个单元格.每个Cell都有一个Label和TextField.
在开始编辑每个部分的索引2处的单元格时,我重新加载现在将包含9个单元格的部分(将模型更新为dequeueCell,以便添加5个单元格).

问题:

tableView按预期滚动(将文本字段显示到屏幕的可见部分)以用于该部分的未展开状态.但是在我通过开始编辑任何部分的索引2处的单元格的文本字段来添加单元格之后,tableView会滚动以隐藏文本字段.一旦任何部分扩展了单元格数量,就会对tableview中的任何单元格进行奇怪的滚动.此外,然发生了奇怪的滚动,但是重新加载了tableView(这导致焦点远离文本字段).我在单元格的didBeginEdiTing(:_)自定义委托中包含了tableView.reloadSection(_ :).
我在iOS 9和10中看到过这个问题

对不起解释不好.谢谢

继承人Github Repo

问题在这里

附:我正在使用Swift 3和Xcode 8.3.3以及部署目标iOS 10

请不要在Swift 4和Xcode 9中建议回答

解决方法

您可以尝试另一种方法:更改单元格的高度而不是插入/删除.

更改单元格数以始终返回所有项目:

override func tableView(_ tableView: UITableView,numberOfRowsInSection section: int) -> Int {
        // #warning Incomplete implementation,return the number of rows
        guard let sectionEnum = Sections(rawValue: section) else { return 0 }

        return sectionEnum.getRows(forExpanded: truE).count
    }

将“隐藏”项的高度设置为0:

override func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
    guard let sectionEnum = Sections(rawValue: indexPath.section) else { return 0 }

    let isExpanded = expandedSectionData[indexPath.section]
    if (!isExpanded) {
        let object = sectionEnum.getRows(forExpanded: truE)[indexPath.row]
        if (!sectionEnum.getRows(forExpanded: falsE).contains(object)) {
            return 0;
        }
    }
    return self.tableView.estimatedRowHeight
}

设置单元格以将子视图剪辑到其边界:

override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
           ....
           cell.clipsToBounds = true;
           return cell
        }

并将更新代码更改为(删除tableView.reloadSections,更改indexPath):

func didBeginEdiTing(textField: UITextField,cell: UITableViewCell) {
        guard let indexPath = tableView.indexPath(for: cell),let section = Sections(rawValue: indexPath.section) else { return }
        if indexPath.row == 7 && !expandedSectionData[indexPath.section] {
            expandedSectionData[indexPath.section] = true
            tableView.beginupdates()
            tableView.endupdates()
            tableView.scrollToRow(at: indexPath,at: UITableViewScrollPosition.none,animated: truE)
            textField.becomeFirstResponder()
        }

}

大佬总结

以上是大佬教程为你收集整理的ios – 添加单元格后TableView Auto Scrolling行为不正常全部内容,希望文章能够帮你解决ios – 添加单元格后TableView Auto Scrolling行为不正常所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。