我有一个问题是在我的集合视图中添加额外的单元格 我已经改变了使用数据模型来填充我的单元格的逻辑
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
}
问题出在这一行我认为
让picture = pictures [indexPath.item]为!廊
当我标记并标记
时cell.image.sd_setImageWithURL(NSURL(string:picture.fileUrl), placeholderImage:UIImage(名称:“占位符”))
在第11位添加额外的细胞。但其他方式让我超越界限
有人可以帮助我吗?
答案 0 :(得分:5)
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
}
答案 1 :(得分:0)
您的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
}