Skip to content

Instantly share code, notes, and snippets.

@fuzz6001
Last active July 5, 2018 12:10
Show Gist options
  • Save fuzz6001/43467c2c2c5e1e88c75db13e7a956e56 to your computer and use it in GitHub Desktop.
Save fuzz6001/43467c2c2c5e1e88c75db13e7a956e56 to your computer and use it in GitHub Desktop.
reloadしたときのvisibleCellsの全てがwillDisplayCellを実行したらreload完了とみなすテスト
/*
reloadしたときのvisibleCellsの全てがwillDisplayCellを実行したらreload完了とみなすテスト
以下の記事に対応しています。
[reloadData処理後に処理を行いたい]
http://qiita.com/ponkichi4/items/d5d46556773a6bc98f9c
Swift2です
*/
@IBOutlet weak var table: UITableView!
var cells = [UITableViewCell]() //visibleCells保存用
(中略)
//ボタン押したらreload
@IBAction func pushedButton(sender: UIButton) {
NSLog("pushed")
//保存
cells = table.visibleCells
//表示してみる
cells.forEach {
if let indexPath = table.indexPathForCell($0) {
NSLog("[visibleCells] %@", indexPath)
} else {
NSLog("unknown cell (nil)")
}
}
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 = cells.indexOf(cell) {
//保存用Arrayから削除
cells.removeAtIndex(index)
NSLog("[willDisplayCell] \(indexPath)")
if cells.count == 0 {
//全て無くなったので完了!!
NSLog("all reloaded")
}
} else {
//reload時以外はここに来る
NSLog("unknown cell")
}
}
@fuzz6001
Copy link
Author

fuzz6001 commented Mar 22, 2017

私の方が遅いこともあれば、

11:22:34.076 testTableReload[543:103015] pushed
11:22:34.078 testTableReload[543:103015] [visibleCells] <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}
11:22:34.078 testTableReload[543:103015] (中略 1~4)
11:22:34.079 testTableReload[543:103015] [visibleCells] <NSIndexPath: 0xc000000000a00016> {length = 2, path = 0 - 5}
11:22:34.080 testTableReload[543:103015] execute reload!
11:22:34.081 testTableReload[543:103015] executed reload
11:22:34.082 testTableReload[543:103015] [addOperationWithBlock] all reloaded
11:22:34.087 testTableReload[543:103015] [willDisplayCell] <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}
11:22:34.088 testTableReload[543:103015] (中略 1~4)
11:22:34.091 testTableReload[543:103015] [willDisplayCell] <NSIndexPath: 0xc000000000a00016> {length = 2, path = 0 - 5}
11:22:34.091 testTableReload[543:103015] all reloaded

ponkichi4さんの方が遅いこともある。

11:24:52.493 testTableReload[543:103015] pushed
11:24:52.494 testTableReload[543:103015] [visibleCells] <NSIndexPath: 0xc000000000400016> {length = 2, path = 0 - 2}
11:24:52.496 testTableReload[543:103015] (中略 3~7)
11:24:52.496 testTableReload[543:103015] [visibleCells] <NSIndexPath: 0xc000000001000016> {length = 2, path = 0 - 8}
11:24:52.497 testTableReload[543:103015] execute reload!
11:24:52.502 testTableReload[543:103015] executed reload
11:24:52.506 testTableReload[543:103015] [willDisplayCell] <NSIndexPath: 0xc000000000400016> {length = 2, path = 0 - 2}
11:24:52.511 testTableReload[543:103015] (中略 3~7)
11:24:52.512 testTableReload[543:103015] [willDisplayCell] <NSIndexPath: 0xc000000001000016> {length = 2, path = 0 - 8}
11:24:52.514 testTableReload[543:103015] all reloaded
11:24:52.517 testTableReload[543:103015] [addOperationWithBlock] all reloaded

@fuzz6001
Copy link
Author

画面外なのにtable.visibleCellsに入ったままなことがあるのでダメですね。(画面を縦→横に回転させた直後など)
失敗です。

11:28:57.043 testTableReload[550:104361] pushed
11:28:57.044 testTableReload[550:104361] [visibleCells] <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}
11:28:57.053 testTableReload[550:104361] (中略 1~10)
11:28:57.053 testTableReload[550:104361] [visibleCells] <NSIndexPath: 0xc000000001600016> {length = 2, path = 0 - 11}
11:28:57.054 testTableReload[550:104361] execute reload!
11:28:57.057 testTableReload[550:104361] executed reload
11:28:57.058 testTableReload[550:104361] [addOperationWithBlock] all reloaded
11:28:57.062 testTableReload[550:104361] [willDisplayCell] <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}
11:28:57.066 testTableReload[550:104361] (中略 1~4)
11:28:57.067 testTableReload[550:104361] [willDisplayCell] <NSIndexPath: 0xc000000000a00016> {length = 2, path = 0 - 5}

@fuzz6001
Copy link
Author

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