我遇到了一个我真的找不到任何答案的问题。 我有一个 collectionView,每次点击按钮时,我都会使用新参数重新加载 collectionView 的数据。我的问题是这样做需要大约 1-2 秒,这非常烦人并且卡住了我的 UI。
我想知道是否有一种方法可以在后台重新加载collectionView并仅在完成时显示它,例如(伪代码):
self.collectionView.hidden = yes;
dispatch_async({
[self.collectionView reloadData];
});
当它完全更新时:
self.collectionView.hidden = no;
我还没有在互联网上找到任何关于如何做到这一点的信息,我真的需要一些帮助。
cellForRowAtIndexPath 方法的完整代码:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = nil;
if (indexPath.row >= 1 && indexPath.row <= 7)
{
static NSString *cellIdentifier = @"AGZDayLetterCell";
cell =(AGZDayLetterCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
((AGZDayLetterCollectionViewCell*)cell).dayFirstLetterLabel.text = [self.daysFirstLetterArray objectAtIndex:(indexPath.row-1)];
cell.userInteractionEnabled = NO;
}
else if (indexPath.row%8 == 0)
{
static NSString *cellIdentifier = @"AGZSeparatorCell";
cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
cell.userInteractionEnabled = NO;
}
else if (indexPath.row > 8+self.delay)
{
NSString *dayNumberTapped = [NSString stringWithFormat:@"%ld", (long)indexPath.row-7-(int)indexPath.row/8-self.delay];
static NSString *cellIdentifier = @"AGZDayCell";
cell =(AGZDayCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
((AGZDayCollectionViewCell*)cell).dateTappedString = [self getDateStringForDayNumber:dayNumberTapped];
NSDate *selectedDayDate = [[self getDateStringForDayNumber:dayNumberTapped] dateFromStringForRFC3339Format];
((AGZDayCollectionViewCell*)cell).dayAppointmentsArray = (NSMutableArray*)[self.appointmentsDictionary objectForKey:[selectedDayDate getDateOnlyRFC3339Format]];
// We check if the cell is today's date and set the design according to the result
if ([[self.viewDate getDateOnly] isEqualToString:[[NSDate date] getDateOnly]] && [[NSDate date] getDay] == [dayNumberTapped intValue])
((AGZDayCollectionViewCell*)cell).isTodayDay = YES;
else
((AGZDayCollectionViewCell*)cell).isTodayDay = NO;
}
else {
static NSString *cellIdentifier = @"AGZDayCell";
cell =(AGZDayCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
((AGZDayCollectionViewCell*)cell).dateTappedString = @"";
cell.userInteractionEnabled = NO;
((AGZDayCollectionViewCell*)cell).isTodayDay = NO;
}
return cell;
}
最佳答案
我认为问题出在 Collection View 上的可能性很小。许多人以各种方式使用它们,并且性能比你得到的要好得多。更有可能的是,生成数据的任何代码都在占用时间。例如 - 击中服务器。这是应该在后台线程上附加回调或完成 block 的代码。
UI 代码永远不应该在后台三上,如果不在主线程上运行,将会导致各种 UI 问题。
关于ios - 由于 dequeueWithReusableIdentifier,UICollectionView 加载速度非常慢,我可以在后台加载它吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29090070/