ios - 由于 dequeueWithReusableIdentifier,UICollectionView 加载速度非常慢,我可以在后台加载它吗?

标签 ios objective-c xcode uicollectionview

我遇到了一个我真的找不到任何答案的问题。 我有一个 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/

相关文章:

ios - Cocos2d。 Sprite 和进度条紧密连接

ios - Xcode 6 - 无法构建模块 'Accelerate'

objective-c - Flac 转换器 Objective-C?

ios - 虽然我在 Storyboard上增加了 UITableViewCell 的高度,但在设备或模拟器上看不到

ios - 如何在 iPhone Xs 模拟器上启用 Siri 的文本输入?

ios - Objective-c:仅限横向

objective-c - 如何使用 sqlite 在 Xcode 中开始、回滚、提交事务

iOS键盘改变字体,大小,颜色

objective-c - 将 NSData 转换为 NSString 时为空结果

iphone - UITableview,其中包含按排序顺序排列的名称部分