Skip to content

Instantly share code, notes, and snippets.

@fuzz6001
Created March 24, 2017 05:04
Show Gist options
  • Save fuzz6001/94ab2586c7c8347df116b95958c776cc to your computer and use it in GitHub Desktop.
Save fuzz6001/94ab2586c7c8347df116b95958c776cc to your computer and use it in GitHub Desktop.
reloadしたときのindexPathsForVisibleRowsの全てがwillDisplayCellを実行したらreload完了とみなすテスト
/*
reloadしたときのindexPathsForVisibleRowsの全てがwillDisplayCellを実行したらreload完了とみなすテスト
以下の記事に対応しています。
[reloadData処理後に処理を行いたい]
http://qiita.com/ponkichi4/items/d5d46556773a6bc98f9c
Swift2です
*/
@IBOutlet weak var table: UITableView!
var indexPathsForVisibleRows: [NSIndexPath]? //保存用
(中略)
//ボタン押したらreload
@IBAction func pushedButton(sender: UIButton) {
NSLog("pushed")
//保存
indexPathsForVisibleRows = table.indexPathsForVisibleRows
//表示してみる
indexPathsForVisibleRows?.forEach {
NSLog("[indexPathsForVisibleRows] %@", $0)
}
NSLog("execute reload!")
table.reloadData()
//ponkichi4さんのアイデア
NSOperationQueue.mainQueue().addOperationWithBlock {
NSLog("[addOperationWithBlock] all reloaded")
}
NSLog("executed reload")
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if let index = indexPathsForVisibleRows?.indexOf(indexPath) {
//保存用Arrayから削除
indexPathsForVisibleRows?.removeAtIndex(index)
NSLog("[willDisplayCell] \(indexPath)")
if indexPathsForVisibleRows?.count == 0 {
//全て無くなったので完了!!
NSLog("all reloaded")
}
} else {
//reload時以外はここに来る
NSLog("unknown cell")
}
}
@fuzz6001
Copy link
Author

縦→横回転後でも大丈夫。

14:10:06.397 testTableReload[2258:296170] pushed
14:10:06.398 testTableReload[2258:296170] [indexPathsForVisibleRows] <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}
14:10:06.398 testTableReload[2258:296170] (中略 1~4)
14:10:06.400 testTableReload[2258:296170] [indexPathsForVisibleRows] <NSIndexPath: 0xc000000000a00016> {length = 2, path = 0 - 5}
14:10:06.400 testTableReload[2258:296170] execute reload!
14:10:06.412 testTableReload[2258:296170] executed reload
14:10:06.413 testTableReload[2258:296170] [addOperationWithBlock] all reloaded
14:10:06.416 testTableReload[2258:296170] [willDisplayCell] <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}
14:10:06.418 testTableReload[2258:296170] (中略 1~4)
14:10:06.422 testTableReload[2258:296170] [willDisplayCell] <NSIndexPath: 0xc000000000a00016> {length = 2, path = 0 - 5}
14:10:06.423 testTableReload[2258:296170] all reloaded

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment