IOS delay loading pictures in cell in TableView

Source: Internet
Author: User

TableView image Delay loading is the content of this article, often we will use TableView display Many items, sometimes need to display pictures . But one-time from the server to fetch all the pictures to the user to waste traffic, the server is also a burden, it is best to load on demand, that is, when the user to browse the item and then go to load often we will use TableView Displays many entries.

Sometimes the picture needs to be displayed, but it takes all the pictures from the server at once to waste traffic to the user, which is also a burden on the server. It is best to load it on demand, that is, when the user wants to browse for the item and then loads its picture.

Override the following methods

  
  1. -(void) TableView: (UITableView *) TableView Willdisplaycell: (UITableViewCell *) cell Forrowatindexpath: (Nsindexpath *) Indexpath
  2. {
  3.     uiimage * image  = [self getimageforcellatindexpath:indexpath];  //get pictures from the internet  
  4. [Cell.imageview Setimage:image];
  5. }

This solves the problem of delayed loading, but when the network speed is slow, or the picture is large (assuming, although the figure in the cell is very small), you will notice that the program may lose its response to the user.

Reason is

  
    1. < Span style= "Font-family:comic Sans MS; font-size:18px ">UIImage *  = [self  getimageforcellatindexpath:indexpath]; 

This method may take a significant amount of time, and the main thread will have to deal with it, so it loses its response to the user.

So the method should be proposed:

  
  1. -(void) Updateimageforcellatindexpath: (Nsindexpath *) Indexpath
  2. {
  3.     nsautoreleasepool * pool  = [[nsautoreleasepool alloc] init];  
  4.     uiimage * image  = [self getimageforcellatindexpath:indexpath];  
  5.     uitableviewcell * cell  = [self.tableview cellforrowatindexpath:indexpath];  
  6. [Cell.imageview performselectoronmainthread: @selector (setimage:) withobject:image Waituntildone:no];
  7. [Pool release];
  8. }

And then open up a new thread to do this thing.

  
  1. -(void) TableView: (UITableView *) TableView Willdisplaycell: (UITableViewCell *) cell Forrowatindexpath: (Nsindexpath *) Indexpath
  2. {
  3. [Nsthread detachnewthreadselector: @selector (updateimageforcellatindexpath:) totarget:self Withobject:indexpath];
  4. }

Similarly, when we need to calculate for a long time, we also have to open a new thread to do this calculation to avoid the program in the suspended animation state

IOS delay loading pictures in cell in TableView

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.