LoginSignup
14
18

More than 5 years have passed since last update.

UICollectionView で 無限スクロール

Last updated at Posted at 2016-01-06

まだ未解決。
Windows の Kobito では下書きや非公開でアップロードできないらしくて公開されてしまった。

参考
Showing cells in demands in UICollectionView with vertical infinite scroll


class CollectionSampleViewController: UICollectionViewController {

    private let reuseIdentifier1 = "Cell"
    private var numberOfItemsPerSection = 9    

    override func viewDidLoad() {
       super.viewDidLoad()        
    }

    override func didReceiveMemoryWarning() {
       super.didReceiveMemoryWarning()
    }

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
       return 1
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return numberOfItemsPerSection
    }    

    override func scrollViewDidScroll(scrollView: UIScrollView) {
       let offsetY = scrollView.contentOffset.y
       let contentHeight = scrollView.contentSize.height

       if offsetY > contentHeight - scrollView.frame.size.height {            
          numberOfItemsPerSection += 6
          self.collectionView.reloadData()
       }
    }   
}

上のコードを説明すると
1. numberOfItemsInSection の値を変化させた後で
2. collectionView.reloadData() を呼び出す。
3. reloadData で再読み込み

reloadData は見えているアイテムを再読み込みさせる。またデータ数が減っている場合はオフセット(スクロール位置)値も調整する。

しかしこれだとセル数が爆発的に増えていくからあかんのでは?

改善案としては、
1. セルデータの基準とする値を保持しておく
2. スクロールが下・上までいった段階で、その基準値を増減させる。
3. indexPath に加算調整したセルデータを読むようにするとかかな?

出来るか知らんけど。

14
18
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
14
18