由于dequeueWithReusableIdentifier,UICollectionView加载速度很慢,我可以在后台加载吗?

时间:2015-03-17 01:56:33

标签: ios objective-c xcode uicollectionview

我面临一个问题,我真的没有找到任何答案。 我有一个collectionView,每次点击一个按钮,我都会使用新参数重新加载collectionView的数据。我的问题是这需要大约1-2s这样做非常烦人并冻结我的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;
}

2 个答案:

答案 0 :(得分:1)

我认为这个问题不太可能是集合视图。许多人以各种方式使用它们,性能远远超过你所获得的。更有可能的是,无论生成数据的代码是花费时间。例如 - 点击服务器。这是应该在附加回调或完成块的后台线程上的代码。

UI代码永远不应该在后台三,如果不在主线程上运行,将导致各种UI问题。

答案 1 :(得分:0)

我建议使用SDWebImage

这有助于我提高CollectionView的性能,如果要动态加载单元格中的图像。