TableView Auto Scrolling Mistakes After Adding Cells

Show settings:

My TableView has 3 sections with 4 or 9 cells each. Each cell has a Label and TextField. On Starting to edit a cell at index 2 of each section, I reload the section, which will now consist of 9 cells (update model for dequeueCell, to add 5 more cells).

Problem:

The TableView scrolls as expected (brings the text box of the visible part of the screen) to the unexpanded state of the section. But after I add cells, starting to edit the cell text field at index 2 of any section, the tableView scrolls so that it hides the text field. A strange scroll occurs for any cells in the table view, as soon as the number of cells in any section is expanded. In addition, while a weird scroll occurs, the tableView reloads (resulting in loss of focus from the text field). I included tableView.reloadSection(_:)in the user delegate didBeginEditing(:_)cell. I saw this problem in iOS 9 and 10

Sorry for the poor explanation. Thanks

Heres Github Repo

And the problem is here Problem Demo

PS I am using Swift 3 and Xcode 8.3.3 to deploy iOS 10

, Swift 4 Xcode 9

+6
3

: /.

, :

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, change 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()
        }

}
+4

, , .

- :

func didBeginEditing(textField: UITextField, cell: UITableViewCell) {
    guard let indexPath = tableView.indexPath(for: cell) else { return }

    if indexPath.row == 2 && !expandedSectionData[indexPath.section] {
        tableView.beginUpdates()
        expandedSectionData[indexPath.section] = true
        tableView.reloadSections(IndexSet(integer: indexPath.section), with: .automatic)
        tableView.endUpdates()

        // after tableview is reloaded, get cell again
        let cell = tableView.cellForRow(at: IndexPath(row: 2, section: indexPath.section)) as? TestCell
        cell?.textField.becomeFirstResponder()
    }
}

, .

+2

. , DidLoad tableView: heightForRowAtIndexPath:.

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 100

,

, ( )

bottomMargin = textField.bottom 750 1000, .

+2

All Articles