Skip to content

Commit

Permalink
Update TableViewDelegate method to be more modern
Browse files Browse the repository at this point in the history
  • Loading branch information
Abizern committed Sep 20, 2017
1 parent db69f3f commit 2e09a6f
Showing 1 changed file with 38 additions and 45 deletions.
83 changes: 38 additions & 45 deletions PartialTable/TableViewController.swift
Expand Up @@ -9,59 +9,52 @@
import UIKit

class TableViewController: UITableViewController {

fileprivate var tableDataSource = DataSource()

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableView.dataSource = tableDataSource
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.section {

case 1:
let numberOfItems = tableDataSource.tableView(tableView, numberOfRowsInSection: 0)
tableDataSource.getMoreItems()

let newNumberOfItems = tableDataSource.tableView(tableView, numberOfRowsInSection: 0)
let shouldHaveMoreButton = tableDataSource.numberOfSections(in: tableView) == 2
var newIndexPaths = [IndexPath]()
for n in numberOfItems ..< newNumberOfItems {
newIndexPaths.append(IndexPath(row: n, section: 0))
}

tableView.beginUpdates()

tableView.insertRows(at: newIndexPaths, with: .top)

if !shouldHaveMoreButton {
tableView.deleteSections(IndexSet(integer: 1), with: .top)
}

tableView.endUpdates()

let scrollPointIndexPath: IndexPath

if shouldHaveMoreButton {
scrollPointIndexPath = indexPath
} else {
scrollPointIndexPath = IndexPath(row: numberOfItems - 1, section: 0)
}

DispatchQueue.main.async { () -> Void in
tableView.scrollToRow(at: scrollPointIndexPath, at: .top, animated: true)

guard let selectedIndexPath = tableView.indexPathForSelectedRow else { return }
tableView.deselectRow(at: selectedIndexPath, animated: true)
}

return

default:
return
guard indexPath.section == 1 else { return }

let numberOfItems = tableDataSource.tableView(tableView, numberOfRowsInSection: 0)
tableDataSource.getMoreItems()

let newNumberOfItems = tableDataSource.tableView(tableView, numberOfRowsInSection: 0)
let shouldHaveMoreButton = tableDataSource.numberOfSections(in: tableView) == 2

let newIndexPaths = Array(numberOfItems ..< newNumberOfItems).map { (n) -> IndexPath in
IndexPath(row: n, section: 0)
}

tableView.beginUpdates()

tableView.insertRows(at: newIndexPaths, with: .top)

if !shouldHaveMoreButton {
tableView.deleteSections(IndexSet(integer: 1), with: .top)
}

tableView.endUpdates()

let scrollPointIndexPath: IndexPath

if shouldHaveMoreButton {
scrollPointIndexPath = indexPath
} else {
scrollPointIndexPath = IndexPath(row: numberOfItems - 1, section: 0)
}

DispatchQueue.main.async { () -> Void in
tableView.scrollToRow(at: scrollPointIndexPath, at: .top, animated: true)

guard let selectedIndexPath = tableView.indexPathForSelectedRow else { return }
tableView.deselectRow(at: selectedIndexPath, animated: true)
}
}
}

0 comments on commit 2e09a6f

Please sign in to comment.