我使用Web服务创建了一个小应用程序。问题是当我点击其中一个单元格时,启动其他表视图需要花费时间(大约5-6秒)。我认为我的代码没有优化。帮助我!!
这是我的代码 rootviewcontroller.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(bdvController == nil)
bdvController = [[BookDetailViewController alloc] initWithNibName:@"BookDetailView" bundle:[NSBundle mainBundle]];
if(indexPath.section == 0)
{
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];
bdvController.aBook = aBook;
}
else if(indexPath.section == 1)
{
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row+5*indexPath.section];
bdvController.aBook = aBook;
}
[self.navigationController pushViewController:bdvController animated:YES];
}
其中'aBook'是Book类的对象,具有所有声明对象的属性。Books是一个数组。
bookDetailview。
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
ImageCell *cell = (ImageCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // creating cell from ImageCell.m
if (cell == nil) {
cell = [[[ImageCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; // allocating with frame
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
EmpDictionary=[[NSMutableDictionary alloc]init];
[EmpDictionary setValue:aBook.Url forKey:@"url"];
[EmpDictionary setValue:aBook.Subcatname forKey:@"subcatname"];
[EmpDictionary setValue:aBook.Catname forKey:@"catname"];
EmpArray=[[NSMutableArray alloc]init];
[EmpArray addObject:EmpDictionary];
CGRect myImageRect = CGRectMake(0.0f, 124.0f, 320.0f, 356.0f);
img = [[UIImageView alloc] initWithFrame:myImageRect];
NSString *tempstr=[EmpDictionary objectForKey:@"url"];
tempstr=[tempstr stringByReplacingOccurrencesOfString:@"\n h" withString:@"h"];
self.img.image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:tempstr]]];
NSDictionary *itemAtIndex = [self.EmpArray objectAtIndex:indexPath.row];
[cell setData:itemAtIndex]; //call to 'setData' method to ImageCell.m
NSLog(@"set text of a cell");
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
jscontroller = [[JSONViewController alloc] initWithNibName:@"JSONView" bundle:nil];
EmpDictionary=[[NSMutableDictionary alloc]init];
[EmpDictionary setValue:aBook.Url forKey:@"url"];
[EmpDictionary setValue:aBook.Subcatname forKey:@"subcatname"];
[EmpDictionary setValue:aBook.Catname forKey:@"catname"];
EmpArray=[[NSMutableArray alloc]init];
[EmpArray addObject:EmpDictionary];
EmpDictionary1=[[NSMutableDictionary alloc]init];
[EmpDictionary1 setValue:aBook.Url forKey:@"url"];
jscontroller.jsonItem = [[NSMutableDictionary alloc]init];
jscontroller.jsonItem = EmpDictionary1;
itemAtIndex = [self.EmpArray objectAtIndex:indexPath.row];
[self.navigationController pushViewController:jscontroller animated:YES];
[jscontroller setData:itemAtIndex];
}
答案 0 :(得分:0)
我认为这是因为您正在加载表中的所有图像,这需要一些时间。你需要做的是,并排加载图像。
看一下这个例子:LazyTableImages
答案 1 :(得分:0)
优化代码以懒惰方式加载单元格图像。它将改善滚动性能。 内存也在泄漏,因为你没有解除分配。
我修改了代码......
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
ImageCell *cell = (ImageCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // creating cell from ImageCell.m
if (cell == nil) {
cell = [[[ImageCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; // allocating with frame
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
/**
* Create a method to store all dictionary values...
* Let self.EmpArray contains such data
*/
NSDictionary *itemAtIndex = [self.EmpArray objectAtIndex:indexPath.row];
[cell setData:itemAtIndex]; //call to 'setData' method to ImageCell.m
NSLog(@"set text of a cell");
return cell; }
你的选择方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(jscontroller!=nil){
[jscontroller release];
jscontroller = nil;
}
jscontroller = [[JSONViewController alloc] initWithNibName:@"JSONView" bundle:nil];
jscontroller.jsonItem = [NSDictionary dictionaryWithObjectsAndKeys:abook.Url,@"url",nil];
itemAtIndex = [self.EmpArray objectAtIndex:indexPath.row];
[self.navigationController pushViewController:jscontroller animated:YES];
[jscontroller setData:itemAtIndex];}
尝试在不同的线程上设置图像......