【问题标题】:why attributes properties in layoutAttributesForItemAtIndexPath does not get applied?为什么 layoutAttributesForItemAtIndexPath 中的属性属性没有被应用?
【发布时间】:2014-11-19 13:00:55
【问题描述】:

如果我修改了layoutAttributesForItemAtIndexPath 中的任何属性属性,collectionView 单元格不会被修改

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes *attributes = [super layoutAttributesForItemAtIndexPath:indexPath];

    //this properties does not get applied
    attributes.zIndex = 1;
    attributes.alpha = 0.5;

    return attributes;
}

但是当将属性修改移动到layoutAttributesForElementsInRect 时,它会被应用:

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSArray *attributesArray = [super layoutAttributesForElementsInRect:rect];

    for (UICollectionViewLayoutAttributes *attributes in attributesArray)
    {
        //this properties is getting applied here
        attributes.zIndex = 1;
        attributes.alpha = 0.5;

    }
    return attributesArray;
}

我在UICollectionView 上应用UIPanGestureRecognizer,我想应用这些 我当前正在拖动的单元格的属性。

我可以在拖动调用此方法时调用 layoutAttributesForItemAtIndexPath 方法:

[customLayoutInstance indexOfItemSelected:indexPath.row];

只要我在拖动,就会调用layoutAttributesForItemAtIndexPath,但不会应用属性。

【问题讨论】:

    标签: ios objective-c iphone uicollectionview uipangesturerecognizer


    【解决方案1】:

    你应该重写另外一种方法:

    - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
    {
    
    {
    

    【讨论】:

    • 我在上面的代码中覆盖了它,但它只被调用一次,而且我希望只要我拖动一个单元格就可以调用一个方法
    • layoutAttributesForItemAtIndexPath 用于(例如),重新加载一个特殊的单元格
    • 此方法(layoutAttributesForElementsInRect)在滚动事件中随时调用
    • 如果您不想在 pan 上更改单元格框架,您可以使用 reloadItemsAtIndexPaths
    • 我在 CollectionView 上应用 UIPanGestureRecognizer 并且当我在 layoutAttributesForElementsInRect 内的单元格中拖动时不会被调用。
    猜你喜欢
    • 2021-07-14
    • 2018-12-15
    • 1970-01-01
    • 2021-06-24
    • 2012-12-11
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多