UIScrollViewのアニメーションを待ってセルを取得する
2709 ワード
「UIScrollViewのアニメーションが完了してからセルを取得して処理を実行したい...」という時がある。アニメーションが終わらないと、cellForRow
がnilを返すのでどうにか待ちたい。そういう場合は以下のdelegateメソッドを利用する。
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
// scrollToRowなどのアニメーションメソッドを呼ぶ前に退避した一時変数
guard let activationIndexPath = self.activationIndexPath else {
return
}
// スクロールのアニメーションが終了する前に処理をてもcellForRowがnilで返って来るので
// アニメーション終了後のこのdelegateで処理を行う
let cell = tableView.cellForRow(at: activationIndexPath)
cell.executeYourTask()
}
(UITableViewDelegateはUIScrollViewDelegateを継承しているので、UITableView利用時でもtableView.delegate = self
としていれば効いてくれる)
アニメーションが必要ない場合は、その場で処理を実行すればよい。
tableView.scrollToRow(at: indexPath, at: .bottom, animated: false)
let cell = tableView.cellForRow(at: indexPath)
cell.executeYourTask()
Author And Source
この問題について(UIScrollViewのアニメーションを待ってセルを取得する), 我々は、より多くの情報をここで見つけました https://qiita.com/yosshi4486/items/ad2ce5e75e10157dd2de著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .