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
- -(void) TableView: (UITableView *) TableView Willdisplaycell: (UITableViewCell *) cell Forrowatindexpath: (Nsindexpath *) Indexpath
- {
- uiimage * image = [self getimageforcellatindexpath:indexpath]; //get pictures from the internet
- [Cell.imageview Setimage:image];
- }
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
- < 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:
- -(void) Updateimageforcellatindexpath: (Nsindexpath *) Indexpath
- {
- nsautoreleasepool * pool = [[nsautoreleasepool alloc] init];
- uiimage * image = [self getimageforcellatindexpath:indexpath];
- uitableviewcell * cell = [self.tableview cellforrowatindexpath:indexpath];
- [Cell.imageview performselectoronmainthread: @selector (setimage:) withobject:image Waituntildone:no];
- [Pool release];
- }
And then open up a new thread to do this thing.
- -(void) TableView: (UITableView *) TableView Willdisplaycell: (UITableViewCell *) cell Forrowatindexpath: (Nsindexpath *) Indexpath
- {
- [Nsthread detachnewthreadselector: @selector (updateimageforcellatindexpath:) totarget:self Withobject:indexpath];
- }
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