gpt4 book ai didi

iphone - 调用 setCollectionViewLayout :animated does not reload UICollectionView

转载 作者:太空狗 更新时间:2023-10-30 03:13:33 54 4
gpt4 key购买 nike

我正在使用具有两个自定义布局的 UICollectionView - 它们是 UICollectionViewFlowLayout 的子类

当我的 View 首次加载时,我使用以下方法将其设置为默认 View :

[self.collectionView setCollectionViewLayout:self.tableViewLayout];

这会调用:cellForItemAtIndexPath - 所以没问题。这是在 viewWillAppear 方法中。

然后我使用 UIButton 将布局从当前 View 更改为下一个布局,如下所示:

-(void)changeViewLayoutButtonPressed
{
self.changeLayout = !self.changeLayout;


if (self.changeLayout){

[self.tradeFeedCollectionView setCollectionViewLayout:self.grideLayout animated:YES];

}

else {


[self.tradeFeedCollectionView setCollectionViewLayout:self.tableViewLayout animated:YES];

}
}

在此调用之后 - numberOfItemsInSection 被调用。我在调试器中检查过返回帐户不为零。

在那次调用之后,没有调用其他任何东西,我的 UICollectionView 更改了布局,但是由于未调用 cellForItemAtIndexPath - 单元格设置不正确。

如果我将 [self.collectionView roloadData] 放在 numberOfItemsInSection 中 - 然后调用 cellForItemAtIndexPath 并设置单元格。

我不需要手动调用来重新加载 numberOfItemsInSection 中的数据。谁能告诉我发生了什么事?

编辑

我忘了说我为两个不同的单元格使用了两个不同的 UINib,因为它们需要应用略有不同的布局。这会是个问题吗?谢谢!

编辑2

是的,所以我已经让它几乎按照我想要的方式工作了。这是用于 Collection View 的所有代码以及我如何更改其布局。

自定义布局

这些是 UICollectionViewFlowLayout 的子类 - 我进行子类化的原因仅仅是因为我的 UICollectionView 需要在外观和行为上非常接近 Apple 的预制布局。不过,只是细胞大小不同。

表格 View 布局- 头文件中没有代码。 - BBTradeFeedTableViewLayout.m

@implementation BBTradeFeedTableViewLayout

-(id)init
{
self = [super init];

if (self){

self.itemSize = CGSizeMake(320, 80);
self.minimumLineSpacing = 0.1f;
}

return self;
}
@end

网格布局- 头文件中没有代码。 - BBTradeFeedGridViewLayout.m

   @implementation BBTradeFeedGridViewLayout

-(id)init
{
self = [super init];

if (self){

self.itemSize = CGSizeMake(159, 200);
self.minimumInteritemSpacing = 0.1f;
self.minimumLineSpacing = 0.1f;
}
return self;
}
@end

然后在具有我的 UICollectionViewViewController 中 - 我声明两个自定义布局,如下所示:

@property (strong, nonatomic) BBTradeFeedTableViewLayout *tableViewLayout;
@property (strong, nonatomic) BBTradeFeedGridViewLayout *grideLayout;

然后我为 collectionView 注册了两个 .xib 文件:

  [self.tradeFeedCollectionView registerNib:[UINib nibWithNibName:@"BBItemTableViewCell" bundle:nil] forCellWithReuseIdentifier:@"TableItemCell"];
[self.tradeFeedCollectionView registerNib:[UINib nibWithNibName:@"BBItemGridViewCell" bundle:nil] forCellWithReuseIdentifier:@"GridItemCell"];

然后,我有一个改变布局的 UIButton:

    -(void)changeViewLayoutButtonPressed
{

if (!self.gridLayoutActive){

self.gridLayoutActive = YES;
[self.tradeFeedCollectionView setCollectionViewLayout:self.grideLayout animated:YES];
self.lastLayoutUsed = @"GridLayout";

}

else {

self.gridLayoutActive = NO;

[self.tradeFeedCollectionView setCollectionViewLayout:self.tableViewLayout animated:YES];
self.lastLayoutUsed = @"TableLayOut";
}
[self.tradeFeedCollectionView reloadItemsAtIndexPaths:[self.tradeFeedCollectionView indexPathsForVisibleItems]];

}

最后 - 当 Web 服务调用完成时 - 它调用 [self.tradeFeedCollectionView reloadData];

所以我的 UICollectionView 被重新加载,最后一个方法:

 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{ static NSString *tableCellIdentifier = @"TableItemCell";
static NSString *gridCellIdentifier = @"GridItemCell";

if (indexPath.item < [self.tradeSearchArray count]){

if (self.gridLayoutActive == NO){

BBItemTableViewCell *tableItemCell = [collectionView dequeueReusableCellWithReuseIdentifier:tableCellIdentifier forIndexPath:indexPath];

if ([self.tradeSearchArray count] > 0){

self.toolBarButtomItem.title = [NSString stringWithFormat:@"%d Results", self.searchResult.searchResults];

tableItemCell.gridView = NO;
tableItemCell.backgroundColor = [UIColor whiteColor];
tableItemCell.item = self.tradeSearchArray[indexPath.row];

}
return tableItemCell;
}else

{

BBItemTableViewCell *gridItemCell= [collectionView dequeueReusableCellWithReuseIdentifier:gridCellIdentifier forIndexPath:indexPath];

if ([self.tradeSearchArray count] > 0){

self.toolBarButtomItem.title = [NSString stringWithFormat:@"%d Results", self.searchResult.searchResults];


gridItemCell.gridView = YES;
gridItemCell.backgroundColor = [UIColor whiteColor];
gridItemCell.item = self.tradeSearchArray[indexPath.row];

}

return gridItemCell;
}

上面代码的问题

上面的代码确实有效。然而,细胞的动画效果不佳而且看起来很奇怪,几乎就像发生了刷新一样。这是由于调用:

[self.tradeFeedCollectionView reloadItemsAtIndexPaths:[self.tradeFeedCollectionView indexPathsForVisibleItems]];

但这是我能让它接近我想要的唯一方法。

最佳答案

-setCollectionViewLayout:-setCollectionViewLayout:animated: 不会导致您的 UICollectionView 重新加载其数据。

  • 如果您想更改单元格样式或更新数据,请调用 [self.collectionView reloadData] 并且 UICollectionViewDataSource 协议(protocol)方法将被调用。

  • 如果您想将 UICollectionView 布局更改为另一个布局,请调用 -setCollectionViewLayout:。或者,如果您想更新您的 UICollectionView 布局,只需调用 [self.collectionView.collectionViewLayout invalidateLayout]

单元格布局和数据在 UICollectionView 中是两个不同的东西。

更新
您应该丢弃所有陈旧数据,可能是通过 -reloadData。并计算 UICollectionViewLayout 子类中每个单元格的新帧。覆盖 - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
您决定新的 contentOffset。例如,您可以保留某个可见单元格的 indexPath 并决定滚动到该 indexPathsetContentOffset:frame.origin布局更改后位于同一 indexPath 的新单元格。

关于iphone - 调用 setCollectionViewLayout :animated does not reload UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22475985/

54 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com