1

我正在尝试将我的 collectionView 转换为 IGListKit CollectionView。我收到错误消息:Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: indexPath != nil'

这是包含 collectionView 的 viewController 代码:

import UIKit
import IGListKit
import Firebase
import SAMCache

class NewHomeViewController: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!

    var sectionCategories: [String] = ["Friends Lists", "Friends", "People"]
    var lists = [Media]()

    lazy var adapter: ListAdapter = {
        return ListAdapter(updater: ListAdapterUpdater(), viewController: self, workingRangeSize: 0)
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView.collectionViewLayout = UICollectionViewLayout()
        adapter.collectionView = collectionView
        adapter.dataSource = self

        // check if the user logged in or not
        Auth.auth().addStateDidChangeListener { (auth, user) in
            if let user = user {
                // signed in

                WADatabaseReference.users(uid: user.uid).reference().observeSingleEvent(of: .value, with: { (snapshot) in
                    if let userDict = snapshot.value as? [String : Any] {
                        self.currentUser = User(dictionary: userDict)
                        print("user is signed in2")
                        //load the media
                        if self.currentUser != nil {
                            DispatchQueue.main.async {
                                self.observeMedia()
                            }
                        }
                    }
                })
                print("user is signed in")
            } else {
                print("user is not signed in")
                self.performSegue(withIdentifier: Storyboard.showWelcome, sender: nil)

            }
        }
    }

    func observeMedia() {
        Media.observeNewMedia { (media) in
            print("observed")
            if !self.lists.contains(media) {
                self.lists.insert(media, at: 0)
                print("adding")
                print("\(self.lists.count)")
                self.adapter.performUpdates(animated: true, completion: nil)
            }
        }
    }
}

extension NewHomeViewController: ListAdapterDataSource {
    func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
        let feedItems: [ListDiffable] = lists
        return feedItems
    }
    func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any) -> ListSectionController {
        return postSectionController()
    }
    func emptyView(for listAdapter: ListAdapter) -> UIView? {
        let view = UIView()
        view.backgroundColor = .red
        return view
    }
}

和我的postSectionController

import UIKit
import IGListKit

class postSectionController: ListSectionController {
    var list: Media!
}

extension postSectionController {
    override func numberOfItems() -> Int {
        return 1
    }
    override func sizeForItem(at index: Int) -> CGSize {
        return CGSize(width: 158, height: 203)
    }
    override func cellForItem(at index: Int) -> UICollectionViewCell {

        print("got to cell for item")
        let cell = collectionContext?.dequeueReusableCell(withNibName: "HomeListsCollectionViewCell", bundle: nil, for: self, at: index) as! HomeListsCollectionViewCell

        cell.media = list
        return cell
    }
    override func didUpdate(to object: Any) {
        list = object as? Media
    }
    override func didSelectItem(at index: Int) {

    }
}

我真的不明白为什么会发生这个错误。该lists数组不为空,并且 collectionViewCell 正在从控制台中的单元格打印信息,因此我知道对象 ( Media) 也不为空。这是我第一次实现 IGListKit,所以我不知道我是否遗漏了什么。任何人都可以帮忙吗?

我尝试将我的 collectionView 更改为具有该类的普通视图,IGListCollectionView但这会产生与标题相同的错误。我的 UICollectionViewCell 类和 nib 在普通的 collectionView 中都可以工作

4

1 回答 1

2

是因为你UICollectionViewLayout应该是UICollectionViewFlowLayout

于 2019-02-26T20:13:50.407 回答