V
V
vldmr51502015-05-27 11:53:44
Objective-C
vldmr5150, 2015-05-27 11:53:44

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

3 answer(s)
A
An, 2015-05-27
@Flanker_4

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

M
Maxim Prigozhenkov, 2015-05-27
@Waka_Waka

UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"url"]]];

Well, or sooner.
NSString *address = @"url";
        NSURL *url = [NSURL URLWithString:address];
        NSData *dataImage = [NSData dataWithContentsOfURL:url];
        UIImage *img = [UIImage imageWithData:dataImage];

In the cell:
In general, see if everything is in order with the binding of elements from the storyboard with the code. It happens that you can’t remove the connection, it crashes the application.

P
Petr2008, 2015-06-04
@Petr2008

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 question

Ask a Question

731 491 924 answers to any question