Last active
July 5, 2018 12:10
-
-
Save fuzz6001/43467c2c2c5e1e88c75db13e7a956e56 to your computer and use it in GitHub Desktop.
reloadしたときのvisibleCellsの全てがwillDisplayCellを実行したらreload完了とみなすテスト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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") | |
} | |
} | |
画面外なのに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}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
私の方が遅いこともあれば、
ponkichi4さんの方が遅いこともある。