iPhone由于数据加载而导致导航缓慢

| 我是Objective-c的新手。我的问题是控制器之间的导航缓慢。 当我从一个控制器跳到另一个控制器时,它真的很慢,因为它从服务中获取数据,然后解析并显示它。所有这些大约需要4-5秒。 现在,我已经将所有获取的数据放入DetailController的loadView中。无论如何,我首先显示控制器,然后显示数据。因为到目前为止,当我在根控制器上选择一行时,它显示活动并停留在根控制器上,然后导航到DetailController。 因此,它首先加载数据,然后显示它。无论如何,我可以逆转此过程,先显示控制器,然后显示它。 这就是我在做什么
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

DataCollection *collection = [DataCollection sharedObject];
Product *tempProduct = [[collection popularProducts] objectAtIndex:row] ;

ProductDetailViewController *tempProductDetail = [[ProductDetailViewController alloc] init];

tempProductDetail.homeViewProduct = tempProduct;
[tempProductDetail release];
}
然后是细节控制器
- (void)loadView {
      [super loadView];
      // here\'s all the data loading and parsing ... which takes time
}
//这是下载数据的方法,我正在使用REST服务来获取所有数据。
- (void) getProductData:(NSString*)productId{

NSMutableString *specificURL = [[NSMutableString alloc] initWithString:@\"GetProductPage?id=\"];
[specificURL appendFormat:@\"%@\",productId];
[specificURL appendFormat:@\"&dataSources=&accountId=&companyId=&version=1&cultureCode=sv&currencyId=2&format=json\"];

NSDictionary *serverCategories = [[NSDictionary alloc] initWithDictionary:[appRequestController proceesRequest:specificURL]];
NSArray *categories = [[[serverCategories objectForKey:@\"DataSources\"] objectAtIndex:0] objectForKey:@\"Items\"];
[serverCategories release];

        if ([[productsArray objectAtIndex:j] objectForKey:@\"Name\"]  != [NSNull null]) {
            [product setProductName:[[productsArray objectAtIndex:j] objectForKey:@\"Name\"]];
        }
        else {
            [product setProductName:@\"\"];
        }


        if ([[productsArray objectAtIndex:j] objectForKey:@\"ThumbnailImage\"]  != [NSNull null]) {
            [product setProductImage:[[productsArray objectAtIndex:j] objectForKey:@\"ThumbnailImage\"]];
        }
        else {
            [product setProductImage:@\"\"];
        }


        if ([[productsArray objectAtIndex:j] objectForKey:@\"Price\"]  != [NSNull null]) {

            [product setProductPrice:[[productsArray objectAtIndex:j] objectForKey:@\"Price\"]];      
        }
        else {
            [product setProductPrice:@\"\"];
        }

            [[collection popularProducts] addObject:product];
        [product release];
} 我已复制粘贴的代码垃圾,以便您可以了解如何获取数据。以下是cellForRowAtIndexPath的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *MyIdentifier = @\"mainMenuIdentifier\";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
    [cell setSelectedBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@\"highlightstrip.png\"]]];

}

UILabel *productNameLbl = [[UILabel alloc] initWithFrame:CGRectMake(90, 8, 200,10)];
[productNameLbl setText:[NSString stringWithString:[[[[DataCollection sharedObject] popularProducts]objectAtIndex:indexPath.row] productName]]];
[productNameLbl setFont:[UIFont boldSystemFontOfSize:12.0]];

[cell addSubview:productNameLbl];
[productNameLbl release];

UILabel *productSubLbl = [[UILabel alloc] initWithFrame:CGRectMake(90, 22, 190,40)];
[productSubLbl setText:[NSString stringWithString:[[[[DataCollection sharedObject] popularProducts]objectAtIndex:indexPath.row] productSubHeader]]];
[productSubLbl setTextColor:[UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102/255.0 alpha:1.0]];
productSubLbl.lineBreakMode = UILineBreakModeWordWrap;
productSubLbl.numberOfLines = 3;
[productSubLbl setFont:[UIFont fontWithName:@\"Arial\" size:10]];

[cell addSubview:productSubLbl];
[productSubLbl release];

NSMutableString *url = [[NSMutableString alloc] initWithString:@\"\"];
NSString *baseImageURL = @\"http://samplesite.com/\";
NSString *imagePath = [NSString stringWithString:[[[[DataCollection sharedObject] popularProducts]objectAtIndex:indexPath.row] productImage]];

[url appendString:baseImageURL];
[url appendString:imagePath];
NSURL *imageURL = [NSURL URLWithString:url];

NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *img = [[UIImage alloc] initWithData:data cache:NO];
UIImageView *imageView = [[UIImageView alloc] initWithImage:img];
[imageView setFrame:CGRectMake(10, 10, 70, 70)];
[cell addSubview:imageView];
[imageView release];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
已邀请:
将网址或产品ID传递到详细信息视图。在ProductDetail的viewDidLoad内部,创建一个后台线程来检索您的数据。后台线程完成后,它将把详细信息通知给ProductDetail控制器。那时,您更新了ProductDetailView。 可以使用后台线程获取数据 NSThread NSO操作
将行传递到您的ProductDetailViewController中,并在其viewDidLoad方法中进行提取。您可能还想显示一个微调器(UIActivityIndi​​cator),并且绝对应该阅读后台线程。
要使用户可以使用视图并在一段时间后加载数据,可以使用NSTimer。 只是有一个NSTimer方法,将在viewDidLoad方法中调用为 [NSTimer ScheduledTimerWithTimeInterval:<#(NSTimeInterval)ti#>目标:<#(id)aTarget#>选择器:<#(SEL)aSelector#> userInfo:<#(id)userInfo#>重复:<#(BOOL)yesOrNo #>] 放置一些时间间隔,之后将调用您的加载方法。 希望这可以帮助。

要回复问题请先登录注册