Answer the question
In order to leave comments, you need to log in
How to add an image by link to a UITableView cell?
Tell a beginner in this business how to make sure that in each cell of the table there is an image received by url. The program parses json and receives information by tags, one of which contains a link to a small image. Everything is fine with the text, and add the image to cell fails - error Outlets cannot be connected to repeating content.
Answer the question
In order to leave comments, you need to log in
Do not do as Maxim Prigozhenkov advises, the dataWithContentsOfURL
method is not intended for this. And in the Apple documentation (seemingly got it) it directly emphasizes that the method is not for network requests.
Just use the ready solution SDWebImage .
PS If the question was "understand how to do it yourself", then just search the Internet UIImage async load
Here is the first thing I saw
As for imageView
stackoverflow.com/questions/27142581/outlets-canno...
You see there is no UITableViewCell subclass, but at the same time, in xib'e /storyboard'e you threw views that the standard UITableViewCell knows nothing about
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"url"]]];
NSString *address = @"url";
NSURL *url = [NSURL URLWithString:address];
NSData *dataImage = [NSData dataWithContentsOfURL:url];
UIImage *img = [UIImage imageWithData:dataImage];
I don’t agree with Mr. An, it’s suitable for a layout, the main thing is not to load it into the rendering of the cell, but first.
How would I do.
1. Content processing.
1.1 We collect data for the cell into the cellContent (NSMutableDictionary) dictionary. Instead of a picture, a dummy (I have a small picture with a clock). Let's assume that the picture is in EVERY cell.
1.2 Add cellContent to tableList (NSMutableArray)
Add urlName to urlList (NSMutableArray)
2. Reload table data.
[self.tableView reloadData];
do not forget to specify in the delegate:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return tableList.count; }
3. We load pictures using NSURLSession, but only through the block.
3.1 counter = tableList.count
3.2 for (int i = 1; i < tableList.count; i++) {
3.3 load image:^
image loaded decremented counter--, not 0 started 3.3
overloaded cell number i
}
Sorry for mixing French with Nizhny Novgorod.
There are nuances, small pictures 5-30Kb can be loaded in 3-5 streams, but no more. We load large pictures in a chain one after another.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question