Search code examples
imageuitableviewurldetailview

Display images from URL in a Detailviewcontroller with Tableviewcontroller as a parent


Hard to explain, but I'll try;

I am making an app for showing roadcameras. I have a TableViewController which leads to a DetailViewController. What I want to achieve is that when I click on Row 1 it shows the picture from www.url1.com. If I click on Row 2, it shows the picture from www.url2.com, and so on. I can't seem to figure out how to do this.

I first started by using this code:

_place = @[@"E6 Minnesund",
          @"E6 Heia"];

_url = @[@"http://webkamera.vegvesen.no/kamera?id=450848",
         @"http://webkamera.vegvesen.no/kamera?id=100102"];

But stopped because I couldn't see how I could get this working...

Any ideas?

And please don't just rate the post down because you think its stupid. I'm kinda new to Xcode and for me, this is hard. Thanks in advance

I can post the whole project folder if necessary


Solution

  • just write the code in didselectrowAtIndexPath

    - (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
     DetailViewController *picker = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    picker.row=indexPath.row;
    
    [self.navigationController pushViewController:picker];
    
    }
    

    and in DetailViewController


    in .h


    @interface DetailViewController : UIViewController
    {
    
    }
    @property(nonatomic,assign)int row;
    

    in .m


    - (void)viewDidLoad
    {
    
     dispatch_queue_t myqueue = dispatch_queue_create("myqueue", NULL);
    
        // execute a task on that queue asynchronously
        dispatch_async(myqueue, ^{
    NSURL *url = [NSURL URLWithString:[_url objectAtIndex:row];
    NSData *data = [NSData dataWithContentsOfURL:url];
     dispatch_async(dispatch_get_main_queue(), ^{
    Image.image = [UIImage imageWithData:data]; //UI updates should be done on the main thread
    
    UIImageView * myImageView = [[UIImageView alloc] initWithImage:image];
    [self.view addSubview:myImageView];
         });
        });