简体   繁体   中英

how to load images in tableview in ios6 after tableview

I have a table view which is filled with array from json .I also get profile images of the user and that has to be loaded next to the msgs filled by the array .

I have 2 questions here:

1> Do I need to fill the tableview first on the main thread then then fill images in the background which is filled off the main thread or is there any other solution to the above procedure ?

2> How do I load it exactly where the msg is, if the loading of the images takes time and somebody already deletes the msg from the tableview .Will tableview know that the object in that particular cell has been deleted and hence that images which is loading in the background is omitted and proceeds .Or do I need to hold the entire thread on the main thread till the msgs and images are both loaded ?

Please suggest me which is the best solution or solution which you have already implemented .Please consider this question as an enquiry from all the enthusiasts and beginners in tableview ios6 architecture.

Please share Any link/data related to the above question ios6 storyboard architecture.

Thanks in Advance.

1> Do I need to fill the tableview first on the main thread then then fill images in the background which is filled off the main thread or is there any other solution to the above procedure ?

All UI changes must be done on the main thread. But the downloading of the images (and any processing, like if you want to round the corners) should be done on a background thread.

2> How do I load it exactly where the msg is, if the loading of the images takes time and somebody already deletes the msg from the tableview .Will tableview know that the object in that particular cell has been deleted and hence that images which is loading in the background is omitted and proceeds .Or do I need to hold the entire thread on the main thread till the msgs and images are both loaded ?

The table view reflects whatever is in your data source. It doesn't keep track of anything for you except table cells. If a user deletes a message and the image hasn't downloaded yet, you should cancel the download of that image.

For image download, you might want to check out the SDWebImage library which will do lots of this for you. It enables a "setImageWithURL" method, which you can use like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:MyIdentifier] autorelease];
    }

    // Here we use the new provided setImageWithURL: method to load the web image
    [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    cell.textLabel.text = @"My Text";
    return cell;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM