【发布时间】:2014-01-06 10:51:00
【问题描述】:
我有一个UICollectionView,可以在其中添加和删除单元格。我正在使用performBatchUpdates 进行这些更改,并且布局的动画效果符合预期。
当我滚动到内容的末尾并删除一个项目以使contentSize 减少时,问题就出现了:这会导致contentOffset 发生变化,但变化不是动画的。相反,contentOffset 在删除动画完成后立即跳转。我尝试在删除的同时手动更新contentOffset,但这对我也不起作用。
我正在使用自定义布局,但我看到与标准流布局相同的行为,使用以下代码:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.items.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UILabel *label = (UILabel *)[cell viewWithTag:1];
label.text = [self.items objectAtIndex:indexPath.item];
return cell;
}
- (IBAction)addItem:(UIBarButtonItem *)sender
{
self.runningCount++;
[self.items addObject:[NSString stringWithFormat:@"Item %u",self.runningCount]];
[self.collectionView performBatchUpdates:^{
[self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:self.items.count-1 inSection:0]]];
} completion:nil];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[self.items removeObjectAtIndex:indexPath.item];
[self.collectionView performBatchUpdates:^{
[self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
} completion:nil];
}
我觉得我一定遗漏了一些明显的东西,但这让我很难过。
【问题讨论】:
-
我希望你不介意我问,但我想知道你是否遇到过类似的情况,删除一个项目,如
contentSize缩小 & 视图滚动导致滚动到视图项目直到删除动画完成后才出现?重现的代码与您发布的代码几乎相同,所以我想您一定也看到了?我已经posted a question 解释过了;如果您有时间看一看,我将不胜感激。
标签: ios objective-c uicollectionview contentsize contentoffset