尝试将2个额外的单元格添加到集合视图“添加更多”和“占位符”

时间:2016-10-31 01:18:39

标签: ios swift uicollectionview

由于某种原因,我的收藏视图不会返回正确数量的项目。我正在尝试在我的集合视图中添加2个占位符单元格,因此我在数组计数中添加了额外的2。

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if IGAccounts.isEmpty {
        print("its empty")
        return 2
    } else {
        print("its not empty: \(IGAccounts.count)")
        return IGAccounts.count + 2
    }
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    print("should be called three times")
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "IGLoginCollectionViewCell", for: indexPath) as! IGLoginCollectionViewCell

    if IGAccounts.isEmpty {
        if indexPath.item == 0 {
            cell.type = .Login
        } else if indexPath.item == 1 {
            cell.type = .Placeholder
        }
    } else {
        print("the count: \(IGAccounts.count) the row: \(indexPath.item)")
        if indexPath.item == IGAccounts.count + 1 {
            cell.type = .Add
        } else if indexPath.item == IGAccounts.count {
            cell.type = .Placeholder
        } else {
            cell.type = .Created
        }
    }
    return cell
}

这是加载View控制器并添加帐户后的日志:

its empty
its empty
should be called three times
should be called three times
push IG login
its not empty: 1
should be called three times
the count: 1 the row: 0
should be called three times
the count: 1 the row: 1

被困在这几个小时。如果有人知道我做错了什么,我将不胜感激!

0 个答案:

没有答案