IGListKit icon indicating copy to clipboard operation
IGListKit copied to clipboard

NestedAdapters example scrolls two cells at same time

Open doston0000 opened this issue 5 years ago • 1 comments

New issue checklist

  • [x] I have reviewed the README and documentation
  • [x] I have searched existing issues and this is not a duplicate
  • [x] I have attempted to reproduce the issue and include an example project.

General information

  • IGListKit version:4.0.0
  • iOS version(s):14.3
  • CocoaPods/Carthage version:
  • Xcode version:12.3
  • Devices/Simulators affected:
  • Reproducible in the demo project? (Yes/No):Yes
  • Related issues:

Debug information

I am trying to create same ui like in instagram app, in demo project i opened NestedAdapter example and changed it little bit, added paging enabled and changed size of cells to fit width, but the problem is whenever i scroll top element, somehow my bottom element also scrolls to that position. Any help will be appreciated)

NestedAdapterViewController.swift


import IGListKit
import UIKit

final class NestedAdapterViewController: UIViewController, ListAdapterDataSource {

    lazy var adapter: ListAdapter = {
        return ListAdapter(updater: ListAdapterUpdater(), viewController: self)
    }()
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())

    let data: [Any] = [
        "Ridiculus Elit Tellus Purus Aenean",
        "Condimentum Sollicitudin Adipiscing",
        14,
        "Ligula Ipsum Tristique Parturient Euismod123132",
        "Purus Dapibus Vulputate22",
        6,
        "Tellus Nibh Ipsum Inceptos1231",
        2,"Ligula Ipsum Tristique Parturient Euismod1231233",10]

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(collectionView)
        adapter.collectionView = collectionView
        adapter.dataSource = self
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        collectionView.frame = view.bounds
    }

    // MARK: ListAdapterDataSource

    func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
        return data as! [ListDiffable]
    }

    func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any) -> ListSectionController {
        if object is Int {
            return HorizontalSectionController()
        } else {
            return LabelSectionController()
        }
    }

    func emptyView(for listAdapter: ListAdapter) -> UIView? {
        return nil
    }

}

HorizontalSectionController.swift


import IGListKit
import UIKit

final class HorizontalSectionController: ListSectionController, ListAdapterDataSource {

    private var number: Int?

    lazy var adapter: ListAdapter = {
        let adapter = ListAdapter(updater: ListAdapterUpdater(),
                                  viewController: self.viewController)
        adapter.dataSource = self
        return adapter
    }()

    override func sizeForItem(at index: Int) -> CGSize {
        return CGSize(width: collectionContext!.containerSize.width, height: collectionContext!.containerSize.width)
    }

    override func cellForItem(at index: Int) -> UICollectionViewCell {
        guard let cell: EmbeddedCollectionViewCell = collectionContext?.dequeueReusableCell(
            for: self,
            at: index
        ) else {
            fatalError()
        }
        adapter.collectionView = cell.collectionView
        return cell
    }

    override func didUpdate(to object: Any) {
        number = object as? Int
    }

    // MARK: ListAdapterDataSource

    func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
        guard let number = number else { return [] }
        return (0..<number).map { $0 as ListDiffable }
    }

    func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any) -> ListSectionController {
        return EmbeddedSectionController()
    }

    func emptyView(for listAdapter: ListAdapter) -> UIView? {
        return nil
    }

}

EmbeddedSectionController.swift

import IGListKit
import IGListSwiftKit
import UIKit

final class EmbeddedSectionController: ListSectionController {

    private var number: Int?

    override init() {
        super.init()
        self.inset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }

    override func sizeForItem(at index: Int) -> CGSize {
        let height = collectionContext?.containerSize.width ?? 0
        return CGSize(width: height, height: height)
    }

    override func cellForItem(at index: Int) -> UICollectionViewCell {
        guard let cell: CenterLabelCell = collectionContext?.dequeueReusableCell(for: self, at: index) else {
            fatalError()
        }
        let value = number ?? 0
        cell.text = "\(value + 1)"
        cell.backgroundColor = UIColor(red: 237 / 255.0, green: 73 / 255.0, blue: 86 / 255.0, alpha: 1)
        return cell
    }

    override func didUpdate(to object: Any) {
        number = object as? Int
    }

}

doston0000 avatar Jan 11 '21 12:01 doston0000

top element's cell image

bottom element's cell image

doston0000 avatar Jan 11 '21 12:01 doston0000