添加额外的单元格为"查看更多"如果计数是> 8在集合视图单元格中

时间:2016-04-28 11:13:37

标签: ios swift uicollectionview

我有一个UICollectionView。我从我的URL动态显示我的集合视图中的单元格。我设置单元格的宽度和高度,单行中只有4个单元格。所以现在从我的网址,我得到了14件物品。我的意思是14个标签名称和14个标签。因此,我的表格视图中必须显示14个项目。

但我需要的是:我需要在我的集合视图中只显示两行,并设置单元格的宽度和高度。所以每行4项意味着,两行共8项。但是从我的网址中我得到了14个项目吗?。

所以,我需要的是 - 如果项目数量超过7.我计算从0到7的形式。然后我需要将第7个单元格显示为静态。文本应自动更改为“查看更多”

怎么做?

baz.cshtml

1 个答案:

答案 0 :(得分:1)

首先不要返回BTdata.count,如果超过8,则返回8.

return BTdata.count>8 ?  8 : BTdata.count;

接下来在UI编辑器中添加另一种类型的单元格,给它起一个名字(比如说SeeMore),然后修改集合视图项处理程序,使它返回最后一行的相应值:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
  if indexPath.row==7 {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("SeeMore", forIndexPath: indexPath)
    return cell       
  } else {
    let cell: collview1 = collectionView.dequeueReusableCellWithReuseIdentifier("Cell1", forIndexPath: indexPath) as! collview1
    cell.lblCellA.text = BTdata[indexPath.row].BTNames
    cell.imgCellA.image = UIImage(named: tableImages[indexPath.row])
    return cell
  }
}

并在collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize方法中相应地返回该单元格的相应大小。