开发者

tableview cell is responding very slow on tap

I have created the small application using the web services. The problem is when i tap on one of the cell, it takes time to launch other table view (approx 5-6 seconds) .I think my code is not optimized. help me!!

here is my code 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];
}

where 'aBook' is object of Book class having property of all its declared object.And Books is an array.

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 *)ind开发者_C百科exPath {
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];       
 }


Optimize your code to load the cell images in lazy manner. It'll improve scroll performance. Also memory is leaking, since you are not deallocating it.

I had modified the code ...

- (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; }  

And your didselect method

- (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];}

And try to set image on different thread....


I think this is so because you are loading all the images in the table at once which takes some time. What you need to do is, load images side by side.

Have a look at this example : LazyTableImages

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜