【问题标题】:CollectionView Add extra cell "Add more"CollectionView 添加额外的单元格“添加更多”
【发布时间】:2016-03-15 21:27:49
【问题描述】:

在我的收藏视图中添加额外的单元格时遇到问题 我已更改逻辑以使用数据模型来填充我的单元格

 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictures.count + 1
    }


    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("images", forIndexPath: indexPath) as! ProfileImageCell

        let picture = pictures[indexPath.item] as! Gallery

        print(indexPath.item)

        if indexPath.item == pictures.count {
            cell.image.image = UIImage(named: "ProfilePlusImage")
        } else {
            cell.image.sd_setImageWithURL(NSURL(string: picture.fileUrl), placeholderImage: UIImage(named: "Placeholder"))
            cell.imageId = picture.id.integerValue
        }

        return cell
    }

我认为问题出在这一行

让图片=图片[indexPath.item]为!图库

当我标记它并标记时

cell.image.sd_setImageWithURL(NSURL(string: picture.fileUrl), placeholderImage: UIImage(named: "Placeholder"))

它在第 11 位添加了额外的单元格。但其他方式让我超越了界限

谁能帮帮我?

【问题讨论】:

  • 是的,在将indexPath.item 用作pictures 的索引之前,您应该验证它是否小于pictures.count。你似乎有了答案。
  • @i_am_jorf no still index 10 beyond bounds [0 .. 9]
  • 只需将该行移到else 语句中
  • @Paulw11 谢谢它的工作,我花了 2 个小时才明白什么坏了:)

标签: ios swift uicollectionview cell uicollectionviewcell


【解决方案1】:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("images", forIndexPath: indexPath) as! ProfileImageCell

    print(indexPath.item)

    if indexPath.item == pictures.count {
        cell.image.image = UIImage(named: "ProfilePlusImage")
    } else {
        let picture = pictures[indexPath.item] as! Gallery

        cell.image.sd_setImageWithURL(NSURL(string: picture.fileUrl), placeholderImage: UIImage(named: "Placeholder"))
        cell.imageId = picture.id.integerValue
    }

    return cell
}

【讨论】:

    【解决方案2】:

    您的 CollectionView 似乎只有一个部分,所以这段代码应该可以工作

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 2
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 0 {
            return 1
        }
        return pictures.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("images", forIndexPath: indexPath) as! ProfileImageCell
    
        let picture = pictures[indexPath.item] as! Gallery
    
        print(indexPath.item)
    
    
        if indexPath.section == 0 {
            cell.image.image = UIImage(named: "ProfilePlusImage")
        } else {
            cell.image.sd_setImageWithURL(NSURL(string: picture.fileUrl), placeholderImage: UIImage(named: "Placeholder"))
            cell.imageId = picture.id.integerValue
        }
    
        return cell
    }
    

    【讨论】:

    • 此代码无法编译,因为print(indexPath.item) 之后的独立if 语句
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多