简体   繁体   English

缩短分段的UITableView的加载时间

[英]Improve Load Time of Sectioned UITableView

I am displaying a UITableView modally, but it takes about two seconds for it to appear, below is the code that is holding up the transition. 我以模态显示UITableView,但是它需要大约两秒钟的时间才能显示,下面是阻止过渡的代码。

ModalViewController.m: ModalViewController.m:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // get all songs from iTunes library
    MPMediaQuery *songQuery = [MPMediaQuery songsQuery];
    // put the songs into an array 
    self.songsArray = [songQuery items];
    // create a sectioned array where songs are sectioned by title
    self.sectionedSongsArray = [self partitionObjects:self.songsArray collationStringSelector:@selector(title)];
}

- (NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector
{
    UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
    NSInteger sectionCount = [[collation sectionTitles] count];
    NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount];
    for(int i = 0; i < sectionCount; i++)
    {
        [unsortedSections addObject:[NSMutableArray array]];
    }
    for (id object in array)
    {
        NSInteger index = [collation sectionForObject:object collationStringSelector:selector];
        [[unsortedSections objectAtIndex:index] addObject:object];
    }
    NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount];
    for (NSMutableArray *section in unsortedSections)
    {
        [sections addObject:[collation sortedArrayFromArray:section collationStringSelector:selector]];
    }
    return sections;
}

The above code works fine, but its slow to load the modal view first time, is there a better way to do this? 上面的代码可以正常工作,但是第一次加载模态视图的速度较慢,是否有更好的方法呢? Thanks. 谢谢。

Yeah: don't do it in -viewDidLoad . 是的:不要在-viewDidLoad这样做。 A better place would be in the view controller's -init or -initWithNibNamed:bundle: or whatever, and in the background. 更好的地方是在视图控制器的-init-initWithNibNamed:bundle:或其他任何东西中,以及在后台。 Example: 例:

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

        dispatch_async(dispatch_get_global_queue(DISPATCH_PRIORITY_DEFAULT, 0), ^{
            // since it's not on the main thread, you need to create your own autorelease pool to prevent leaks
            NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

            MPMediaQuery *songQuery = [MPMediaQuery songsQuery];
            self.songsArray = [songQuery items];            
            self.sectionedSongsArray = [self partitionObjects:self.songsArray collationStringSelector:@selector(title)];

            // UI calls have to be on the main thread, so we go back to that here
            dispatch_async(dispatch_get_main_queue(), ^{
                if([self isViewLoaded])
                {
                    [self.tableView reloadData];
                }
            });

            // this releases any objects that got autoreleased earlier in the block
            [pool release];
        });
    }

    return self;
}

Your -tableView:numberOfRowsInSection: method should of course now check whether sectionedSongsArray is non- nil and in that case return 0 (or 1 if you want to display a “loading” cell, which you probably should). -tableView:numberOfRowsInSection:方法当然应该立即检查是否sectionedSongsArray是非nil ,并在这种情况下返回0(或1,如果你想显示“加载”电池,这你应该)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM