Skip to content

Instantly share code, notes, and snippets.

@Th3nion
Last active October 6, 2017 12:48
Show Gist options
  • Save Th3nion/1744832760de7f9fcc6bb0b775e5af1f to your computer and use it in GitHub Desktop.
Save Th3nion/1744832760de7f9fcc6bb0b775e5af1f to your computer and use it in GitHub Desktop.
UITableView extension wich allow to keep content view at the bottom of the cells.
//
// UITableView+insertScroll.swift
//
// Created by mtournier on 13/09/17.
// NO Copyright !!!
//
import UIKit
extension UITableView {
func insertRowsWithScroll(at indexPath:IndexPath){
let visiblePaths = self.indexPathsForVisibleRows
var bottomPath = visiblePaths?.last
let bRow = bottomPath?.row
let bSection = bottomPath?.section
let row = indexPath.row
let section = indexPath.section
let nextRowInSameSection = (section == bSection) && (row == (bRow! + 1))
let firstRowInNextSection = ((section == (bSection! + 1)) && (row == 0));
let shouldScroll = nextRowInSameSection || firstRowInNextSection;
self.insertRows(at: [indexPath], with: UITableViewRowAnimation.right)
if (shouldScroll) {
self.scrollToRow(at: indexPath, at: .bottom, animated: true)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment