开发者

tableview not so responsive

开发者 https://www.devze.com 2023-02-28 13:30 出处:网络
My app is too slow in responding, I guess I\'m not loading images Asynchronously, but even if I do still then the tableview makes my controller load too slow. The code for tableview is below:

My app is too slow in responding, I guess I'm not loading images Asynchronously, but even if I do still then the tableview makes my controller load too slow. The code for tableview is below:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
    UILabel *productNameLbl = [[UILabel alloc] initWithFrame:CGRectMake(83, 8, 200,10)];
        [productNameLbl setFont:[UIFont boldSystemFontOfSize:12.0]];
        [productNameLbl setTag:20];
        [cell.contentView addSubview:productNameLbl];
        [productNameLbl release];


}

else {
    AsyncImageView *oldImage = (AsyncImageView*)[cell.contentView viewWithTag:299];
    [oldImage removeFromSuperview];
}


NSString *baseImageURL = @"http://samplesite.com/";

AsyncImageView* asyncImage = [[[AsyncImageView alloc] initWithFrame:CGRectMake(5, 5, 70, 70)] autorelease];
asyncIma开发者_如何学Cge.tag = 299;
NSMutableString *url = [[NSMutableString alloc] initWithString:@""];
NSString *iconUrl = [NSString stringWithString:[[[[[DataCollection sharedObject] categoryProducts ] productsList ]objectAtIndex:indexPath.row] productImage]];

[url appendString:baseImageURL];
[url appendString:iconUrl];
NSURL *imageURL = [NSURL URLWithString:url];
[asyncImage loadImageFromURL:imageURL];
[cell.contentView addSubview:asyncImage];

[(UILabel *) [cell viewWithTag:20] setText:[NSString stringWithString:[[[[[DataCollection sharedObject] categoryProducts ] productsList ]objectAtIndex:indexPath.row] productName]]];
//..... rest of the code

What I actually want is, to load the view first and then can load the images and rest of the data. Please, tell me how can I make my tableview more responsive.


You are createing all the Label every time the cell gets displayed. You should either add tag to all the label and locate them via the tag. So not to create them.

Or better create a custom tabelCell with all the controls you need.

You can laod the image async in the UITableViewDelegate methods willDisplayCell:AtIndex: then check some kind of local cache if the images has already been downloaded. Have a look at ASIHttpRequest which has a great caching system which is real easy to use.


If you make this whole part of your code aysnc, your code would be very smooth (mind your leaks + allocations though).

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


this was much easier and really solved the problem :) http://ezekiel.vancouver.wsu.edu/~wayne/yellowjacket/YellowJacket.zip


Apple has a LazyTableImages project that deals with this issue specifically. Perhaps this model can help you out.

0

精彩评论

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

关注公众号