LoginSignup
7
5

More than 3 years have passed since last update.

SkeletonView

Posted at

よく商用アプリでも使われているのを見るローディングライブラリである「SkeletonView」を使用したので、使い方の備忘録として書き残しておきます。

import SkeletonView
    override func viewDidLoad() {
        super.viewDidLoad()
        // TableViewの行の高さを可変にする
        tableView.rowHeight = UITableView.automaticDimension
        // UITableView.automaticDimensionを設定している場合に、SkeletonViewを使うなら
        // estimatedRowHeightの設定が必要
        tableView.estimatedRowHeight = 140
        // スケルトン表示開始
        view.showAnimatedGradientSkeleton()
    }
            defer {
                // スケルトン表示終了
                self.view.hideSkeleton()
            }
// MARK: - SkeletonTableViewDataSource
extension ViewController: SkeletonTableViewDataSource {
    /// スケルトン表示時のセクション数
    func numSections(in collectionSkeletonView: UITableView) -> Int {
        return 2
    }
    /// スケルトン表示時の行数
    func collectionSkeletonView(_ skeletonView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    /// スケルトン表示する再利用セルのIdentifier
    func collectionSkeletonView(_ skeletonView: UITableView, cellIdentifierForRowAt indexPath: IndexPath) -> ReusableCellIdentifier {
        switch indexPath.row {
        case 0:
            return "summaryCell"

        case 1...:
            return "detailCell"

        default:
            DispatchQueue.main.async {
                self.showAlert(title: "予期せぬエラー", message: "予期しないセルがあります")
            }
            return ""
        }
    }
}

参考

7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5