Skip to content

Instantly share code, notes, and snippets.

@ryokosuge
Created April 24, 2015 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryokosuge/0659201499eb18d2aaa8 to your computer and use it in GitHub Desktop.
Save ryokosuge/0659201499eb18d2aaa8 to your computer and use it in GitHub Desktop.
【Swift】UITableViewにindexPathをinsertした後scrollさせる ref: http://qiita.com/ryokosuge/items/03db64cf190ec3f4c470
class DataSource: UITableViewDataSource {
// UITableViewDataSourceの処理は今回はいらないので省略
// handlerは追加する分のNSIndexPathの配列を返す
func reloadData(#handler: ([NSIndexPath] -> Void)) {
// ここでAPIとごにょごにょ
var indexPaths:[NSIndexPath] = []
//
// NSIndexPathを追加する処理
//
handler(indexPaths)
}
// handlerは追加する分のNSIndexPathの配列を返す
func addData(#handler: ([NSIndexPath] -> Void)) {
// ここでAPIとごにょごにょ
var indexPaths:[NSIndexPath] = []
//
// NSIndexPathを追加する処理
//
handler(indexPaths)
}
}
extension ViewController {
private func insertAtIndexPaths(indexPaths: [NSIndexPath]) {
CATransaction.begin()
CATransaction.setCompletionBlock({
self?.loading = false
})
self?.tableView.beginUpdates()
self?.tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .Bottom)
self?.tableView.endUpdates()
if let firstIndexPath = indexPaths.first {
self?.tableView.scrollToRowAtIndexPath(firstIndexPath, atScrollPosition: .Bottom, animated: true)
}
CATransaction.commit()
}
}
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
// これがフラグの変数
private var isLoading: Bool = false
// これは独自クラス
private var dataSource: DataSource = DataSource()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = dataSource
}
}
// MARK: - load data
extension ViewController {
private func reloadData() {
isLoading = true
dataSource.reloadData(handler: {[weak self] indexPaths in
self?.insertAtIndexPaths(indexPaths)
})
}
private func addData() {
isLoading = true
dataSource.reloadData(handler: {[weak self] indexPaths in
self?.insertAtIndexPaths(indexPaths)
})
}
}
extension ViewController {
// ここの処理が今回やりたかったことです。
// 今回は追加するindexPathsの頭のNSIndexPathに追加後スクロールするようにしました。
private func insertAtIndexPaths(indexPaths: [NSIndexPath]) {
CATransaction.begin()
CATransaction.setCompletionBlock({
self?.loading = false
})
self?.tableView.beginUpdates()
self?.tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .Bottom)
self?.tableView.endUpdates()
if let firstIndexPath = indexPaths.first {
self?.tableView.scrollToRowAtIndexPath(firstIndexPath, atScrollPosition: .Bottom, animated: true)
}
CATransaction.commit()
}
}
extension ViewController: UITableViewDelegate {
 // UITableViewDelegateの処理は今回はいらないので省略
func scrollViewDidScroll(scrollView: UIScrollView) {
if isLoading || !isViewLoaded() {
return
}
let scrollValue = scrollView.contentSize.height - scrollView.bounds.height
if scrollValue > scrollView.contentOffset.y {
return
}
// 一番下までスクロールしたというこでデータを追加する
addData()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment