是否有可能在collectionview sizeForItemAtIndexPath委托方法中获取单元格标记?

时间:2014-11-26 15:33:44

标签: ios xcode collectionview

当我调用以下委托方法时:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    float cellWidth = 90.0f; float cellHeight = 60.0f;

    int cellType = [[collectionView cellForItemAtIndexPath:indexPath] tag];

    return CGSizeMake(cellWidth, cellHeight);

}

cellType返回0。 [collectionView cellForItemAtIndexPath:]也返回nil

我猜这是因为单元格没有大小,所以没有显示,当它没有显示时,它返回nil(即在创建单元格对象的实例之前调用此方法) )。

有没有办法在collectionView:cellForItemAtIndexPath方法中获取为此项设置的信息?如何在sizeForItemAtIndexPath方法中识别单个单元格类型?

(我计划使用数据源NSArray,但我试图将此逻辑保留在显示对象中,而不是由数据管理。)

3 个答案:

答案 0 :(得分:0)

想到这样的事情:

var cell;

if let (cell = collectionView.cellForItemAtIndexPath(indexPath))
{
   //get the cell's tag and do what is needed.
}

但我不知道cell是否会在sizeForCell方法中返回一个值

答案 1 :(得分:0)

由于细胞重复使用,我认为标签不可靠。我建议使用你在cellForItemAtIndexPath中使用的相同逻辑来识别NSArray中的数据。然后根据数据确定要返回的大小。换句话说,依赖于cellForItemAtIndexPath和sizeForItemAtIndexPath中的数据源,而不是使sizeForItemAtIndexPath依赖于cellForItemAtIndexPath。

答案 2 :(得分:0)

如果您需要先获取细胞,则需要将其出列。因为layoutAttributesForItemAtIndexPath会在itemAtIndexPath之前被调用,这是您获得nil的原因。

其次,只有当您重复使用该视图时,您才会获得项目的标记,其他所有视图都具有相同的标记0。但是,如果您知道哪个标识符用于使单元格出列,请使用该标识符来提供条件高度。