Skip to content

Instantly share code, notes, and snippets.

@tikipatel
Last active November 28, 2018 13:24
Show Gist options
  • Save tikipatel/44afab31f9c8c2a2189d340dbdb3b6bd to your computer and use it in GitHub Desktop.
Save tikipatel/44afab31f9c8c2a2189d340dbdb3b6bd to your computer and use it in GitHub Desktop.
Quick Playground for adding rows to the top of a tableview
import PlaygroundSupport
import UIKit
class TableViewManager: NSObject, UITableViewDataSource, UITableViewDelegate {
weak var tableView: UITableView?
var strings = ["a", "b", "c", "d","e", "f", "g", "h","i", "j", "k", "dl","am", "bn", "co", "dp","aq", "br", "cs", "dt","au", "bv", "cw", "dx","ay", "bz", "caa", "dbb","acc", "bdd", "cee", "dff","agg", "bhh", "cii", "djj","akk", "bll", "cmm", "dnn","aoo", "bpp", "cqq", "zrr"]
func add(data: [String]) {
strings.insert(contentsOf: data, at: 0) // add your data to the top of source
// updateWithScroll(data: data)
updateWithContentOffsset(data: data)
}
func updateWithScroll(data: [String]) {
guard let tableView = tableView else {
return
}
guard let currentVisibleIndexPaths = tableView.indexPathsForVisibleRows else {
return
}
var updatedVisibleIndexPaths = [IndexPath]()
for indexPath in currentVisibleIndexPaths {
let newIndexPath = IndexPath(row: indexPath.row + data.count, section: indexPath.section)
updatedVisibleIndexPaths.append(newIndexPath)
}
tableView.reloadData()
tableView.scrollToRow(at: updatedVisibleIndexPaths[0], at: .top, animated: false)
}
// Will probably get extremely complicated with dynamic cell height
func updateWithContentOffsset(data: [String]) {
guard let tableView = tableView else {
return
}
let currentOffset = tableView.contentOffset
let yOffset = CGFloat(data.count) * tableView.rowHeight // MAKE SURE YOU SET THE ROW HEIGHT OTHERWISE IT WILL BE ZERO!!!
let newOffset = CGPoint(x: currentOffset.x, y: currentOffset.y + yOffset)
tableView.reloadData()
tableView.setContentOffset(newOffset, animated: false)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return strings.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! Cell
cell.textLabel?.text = strings[indexPath.row]
cell.backgroundColor = .green
return cell
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
print(scrollView.contentOffset.y)
}
}
class Cell: UITableViewCell {
}
let view = UIView(frame: CGRect(x: 0, y: 0, width: 375, height: 667))
view.backgroundColor = .blue
let tableView = UITableView(frame: view.bounds)
view.addSubview(tableView)
tableView.register(Cell.self, forCellReuseIdentifier: "cell")
let tableManager = TableViewManager()
tableManager.tableView = tableView
tableView.dataSource = tableManager
tableView.delegate = tableManager
tableView.rowHeight = 44
let deadlineTime = DispatchTime.now() + .seconds(5)
DispatchQueue.main.asyncAfter(deadline: deadlineTime, execute: {
tableManager.add(data: ["m","j","k"])
print("added data")
})
PlaygroundPage.current.liveView = view
PlaygroundPage.current.needsIndefiniteExecution = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment