Reputation: 6614
I have a table view that's scrolling slowly. Does anyone know why that might be?
There is an image for each row, but even after the images are loaded it still stutters and scrolls slowly.
thanks for any help
here's my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
// Get item from tableData
NSDictionary *item = (NSDictionary *)[displayItems objectAtIndex:indexPath.row];
// display the youdeal deal image
photoString = [item objectForKey:@"image"];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:photoString]]];
cell.titleLabel.text = [item objectForKey:@"supercat"];
cell.descriptionLabel.text = [item objectForKey:@"title"];
NSString *convertedLeftCount = [NSString stringWithFormat:@"%@",[item objectForKey:@"left_count"]];
cell.amountLabel.text = convertedLeftCount;
cell.thumbnailImageView.image = image;
cell.priceLabel.text = [item objectForKey:@"cat"];
return cell;
}
Upvotes: 0
Views: 1098
Reputation: 21
I think You are trying to say this.
NSURL* url = [NSURL URLWithString:@"http://www.YourImageUrl.com"];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse * response,
NSData * data,
NSError * error) {
if (!error){
UIImage* image = [[UIImage alloc] initWithData:data];
// Now workout with the image
}
}];
This will make the asynchronous call, and load the images after tableview loaded i.e when the image load complete the image will show but the table will be loaded when the table view is needed to load.
Upvotes: 0
Reputation: 107121
It's due to the image loading mechanism you used.
You are loading the image from url in the main thread. That's why the UI is blocked for some time also the dataWithContentsOfURL:
is a synchronous call. So the UI will respond after getting the image data.
Apple states that the time taking processes like webrequest,parsing huge data etc must be done on other threads rather than main thread. And all UI related tasks must be done on main thread.
Solutions:
Source code and Third Party Libraries
Here are some links which will help you to understand the basic idea of loaing image using asynchronous methods
Upvotes: 3
Reputation: 3937
The images are getting loaded every time a cell is loaded, because the imageWithData: doesn't use any cache.
Edit: I saw a comment that suggests loading images asynchronously. You already have your custom class for each cell so it should be easy to do it. If it were an answer I'd vote it up
Upvotes: 2