2

When i insert row at index path in uitableview, then my tableview scroll to top? Why?

let indexPathForCell = NSIndexPath(forRow: 5, inSection: 1)
tableView.beginUpdates()
tableView.insertRowsAtIndexPaths([indexPathForCell], withRowAnimation: .Automatic)
tableView.endUpdates()

All code that is invoked during the addition of the cell

func buttonDidPressed(button: CheckMarkView) {
    let indexPathForCell = NSIndexPath(forRow: 5, inSection: 1)
    buttonPressedTag = button.tag
    for checkMark in buttons {
        if checkMark.tag == buttonPressedTag {
            if buttonPressedTag == 4 {
                checkMark.show()
                checkMark.userInteractionEnabled = false
                cellWithCategories["Recomendation"]?.append("slideCell")

                tableView.beginUpdates()
                tableView.insertRowsAtIndexPaths([indexPathForCell], withRowAnimation: .None)
                tableView.endUpdates()
            }
            checkMark.show()
        } else {
            if (tableView.cellForRowAtIndexPath(indexPathForCell) != nil) {
                cellWithCategories["Recomendation"]?.removeLast()
                tableView.beginUpdates()
                tableView.deleteRowsAtIndexPaths([indexPathForCell], withRowAnimation: .None)
                tableView.endUpdates()
            }
            checkMark.hide()
            checkMark.userInteractionEnabled = true
        }
    }
}

code for number of rows :

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let sectionKey = keysForSectionTableView[section]
    let numberOfRows = cellWithCategories[sectionKey]

    return (numberOfRows?.count)!
}
nickheck
  • 79
  • 1
  • 8

1 Answers1

0

I don't see any code that will make your table view scroll to top. But you can try change animation to none. If doesn't work then there is must be some other code, thats causing this issue.

let indexPathForCell = NSIndexPath(forRow: 5, inSection: 1)
tableView.beginUpdates()
tableView.insertRowsAtIndexPaths([indexPathForCell], withRowAnimation: .None)
tableView.endUpdates()
Amit Tandel
  • 883
  • 7
  • 16