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¤cyId=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;
}
没有找到相关结果
已邀请:
3 个回复
财沟项胶
亥套惟间连
芯伶句餐绕