0
votes

I have a tableview (style - grouped) which contains more rows than can fit into its frame so you have to scroll to see the last row. When I delete the last row the tableview scrolls its content down so the new last row is at the bottom of the frame (kind of fill the empty space).

How can I prevent a UITableView from scrolling after I delete the last row in it? I want it to keep the empty space and do not scroll automatically.

I have tried to set content insets but it doesn't seem to prevent scrolling unfortunately.

EDIT: My code for removing the row:

func removeLastRow() {
    let indexPath = IndexPath.init(row: self.messages.count - 1, section: 0)

    self.messages.removeLast()
    CATransaction.begin()
    self.tableView.beginUpdates()
    CATransaction.setCompletionBlock { () -> Void in
       // do stuff, add new rows
    }
    self.tableView.deleteRows(at: [indexPath], with: .left)
    self.tableView.endUpdates()
    CATransaction.commit()
}
1
Assuming you can delete the row, leaving empty space... What do you want to do next? Should the user be able to scroll the table down, then back up to keep the empty space where the row was? Or do you just want it to temporarily be an empty space, and when the user scrolls the "new bottom row" is the bottom row?DonMag
@DonMag I want to keep empty space empty. Like an inset. I will eventually remove inset and insert another row. But if I don't - empty space remains empty. So no complex case when user can scroll to bottom and "new last row" has to become "real last".iur
Maybe instead of deleting the last row, replace it with a blank cell?DonMag

1 Answers

0
votes

Retreive the cells size before deleting by calling tableView:heightForRowAt: and after deleting, set the vertical content offset of your table view manually to the current vertical content offset, plus the height of the cell.

let deletedCellHeight = tableView.heightForRowAt(indexPath)

Then:

tableView.contentOffset.x += deletedCellHeight