繁体   English   中英

重载数据后,UICollectionView单元格保持突出显示

[英]UICollectionView cells stay highlighted after reloaddata

我有一个UICollectionView ,单元格内的标签会定期自动更改。 当此更新触发时,我在UICollectionView上调用reloadData ,并且我已设置单元格以更改[UICollectionViewCell setHighlighted:].上的背景颜色[UICollectionViewCell setHighlighted:].

问题是如果用户按住某个单元格然后进行更新,则当用户释放该单元格时会保持突出显示并且也不能再进行选择。

我注意到dequeueReusableCellWithReuseIdentifier:forIndexPath:在reloadData之后调用单元格上的setHighlighted。

我也尝试过reloadSections:而不是reloadData,这解决了单元格被“卡住”的问题,但是当它被调用时会导致细胞淡出并进入细胞。

将调用放在performBatchUpdates:似乎也无法解决问题。

在单元格的类中尝试调用:

- (void)prepareForReuse {
    [super prepareForReuse];
    [self setHighlighted:NO];
    ... Any other custom stuff that should be cleaned up ...
}

问题是,您可能正在进行不同的背景着色,然后突出显示单元格通常会自行进行。 通常,单元格的超类将撤消prepareForReuse这些更改,但它不知道您的更改。

我使用以下作为解决方法:

// Both highlightedData and lastHighlightedData are only needed if you want to prevent user from selecting a cell which data changed during the reload. If not needed, a boolean may be used instead
@property (nonatomic) id highlightedData;
@property (nonatomic) id lastHighlightedData;
@property (nonatomic) BOOL pendingCollectionViewReload;

// Wrap the reloadData call. Enqueue it if there's a highlighted cell:
- (void)reloadCollectionView
{
    if (self.highlightedData) {
        self.pendingCollectionViewReload = YES;
        return;
    }

    [self.collectionView reloadData];
    self.pendingCollectionViewReload = NO;
}

// When a cell is highlighted, save its index, or better the related data:
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    // Save data at indexPath to self.highlightedData
}    

// Then reload the data when the cell is unhighlighted:
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.lastHighlightedData = self.highlightedData;
    self.highlightedData = nil;

    if (self.pendingCollectionViewReload) {
        [self reloadCollectionView];
    }
}

// The following can be used from shouldPerformSegueWithIdentifier or didSelectItemAtIndexPath to prevent actions if the data associated with the indexPath changed:
- (BOOL)selectedDataEquals:(id)data
{
    // I used lastHighlightedData in addition to highlightedData because this function may be used after the didUnhighlightItemAtIndexPath was called:
    return (self.highlightedData && self.highlightedData == data) || (!self.highlightedData && self.lastHighlightedData == data);
}

我在集合视图中遇到了同样的问题,不需要突出显示,我注意到didHighlight / didUnhighlight方法表现正常,所以我最终在触摸正在进行时阻止重新加载,使用类似这样的东西:

BOOL blockColviewUpdate,colviewUpdateQueued;

接着

-(void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    blockColviewUpdate=YES;
}

-(void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    blockColviewUpdate=NO;
    if(colviewUpdateQueued==YES) [self CollectionViewRefresh];
}

使用自己的函数而不是直接调用reloadData:

-(void)CollectionViewRefresh
{
    if(blockColviewUpdate==YES) colviewUpdateQueued=YES;
    else
    {
        colviewUpdateQueued=NO;
        [self.colview reloadData];
    }
}

它在我的情况下有所帮助,没有重新加载丢失。

我在我最近的一个项目中使用集合视图,以下代码对我来说很好。

    - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:    (NSIndexPath *)indexPath
{
    return YES;
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath;
{
    return YES;
}

在collectionviewcell子类中

@interface SalesLegendCell:UICollectionViewCell

下面是代码

    - (id)initWithFrame:(CGRect)frame {    
    self = [super initWithFrame:frame];    
    if (self) {
        // Initialization code
        NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"SalesLegendCell" owner:self options:nil];

        if ([arrayOfViews count] < 1) {
            return nil;
        }

        if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[SalesLegendCell class]]) {
            return nil;
        }

        self = [arrayOfViews objectAtIndex:0];

        UIView* backgroundView = [[UIView alloc] initWithFrame:self.bounds];
        backgroundView.backgroundColor = [UIColor colorWithWhite:0.85 alpha:1];

        UIView* selectedBGView = [[UIView alloc] initWithFrame:self.bounds];
        selectedBGView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:1];

        self.selectedBackgroundView = backgroundView;
        self.backgroundView = selectedBGView;        
    }
    return self;
}

可能不相关,但这就是我:在.alwaysTemplate模式下在UIViewCollectionCell上呈现的UIImage实例默认突出显示。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM