Ios 为uicollectionview重置indexPath
我有一个集合视图,它看起来像 第一个单元格将保存一些文本,从第二个单元格开始,数据由数据管理器填充 为了处理这个问题,我有这样的代码Ios 为uicollectionview重置indexPath,ios,swift,uicollectionview,swift2,Ios,Swift,Uicollectionview,Swift2,我有一个集合视图,它看起来像 第一个单元格将保存一些文本,从第二个单元格开始,数据由数据管理器填充 为了处理这个问题,我有这样的代码 override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { if(indexPath.row==0){
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if(indexPath.row==0){
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(customCellNibName, forIndexPath: indexPath)
let offer: TGOfferDataSource? = dataSource?.dataforIndexPath(indexPath) as? TGOfferDataSource
cell.setOfferData(offer!)
return cell
}
else if(indexPath.row != 0)
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellNibName, forIndexPath: indexPath)
let product: TGProductDataSource? = dataSource?.dataforIndexPath(indexPath) as? TGProductDataSource
cell.setProductData(product!)
return cell
}
}
问题是data manager的第一个产品隐藏在这个灰色单元格后面
所以我想我必须在elseif部分增加索引路径。elseif部分中类似indexPath的内容也应该从0开始。
(Swift和单元格为xib)
知道怎么做吗
谢谢
看了一下,但是如何在swift中实现这一点
编辑
如果我移除If部分,它看起来像
您可以使用以下命令在swift中创建新的
nsindepath
let path = NSIndexPath(forRow: indexPath.row - 1, inSection: indexPath.section)
您可以将上面的
path
传递到else部分中的dataforIndexPath
。希望有帮助 是否可以添加dataSourcedataforIndexPath
方法?另外,为什么您的产品类名为TGProductDataSource
?@MichaëlAzevedo dataforindexath只返回indexath.item处的数据值,这很明显,TG只是我们的企业命名约定。感谢您的帮助,不管怎样,下面的答案确实是这样的:trickI试图知道您是否需要一个indexpath作为参数,或者仅仅一个索引是否足够,因为看起来您没有使用NSIndexPath.my bad的section属性,从下一次vl尝试发布完整的代码开始