Search code examples
iosuitableviewnsurlconnection

Image not showing using Nsurlconnection Post method


nslog using jsonI am using nsurlconnection in POST method and created four array for "see","buy","updated","image".Displayed in tableviewcell but "image" not showing. image in "url in png format".

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{


    NSError* error;
    json = [NSJSONSerialization JSONObjectWithData: mutableData
                                           options:kNilOptions
                                             error:&error];
    NSLog(@"%@",json);
    sellArray = [json valueForKeyPath:@"BranchByList.sell"];
    buyArray = [json valueForKeyPath:@"BranchByList.buy"];
    updataedArray = [json valueForKeyPath:@"BranchByList.updated"];
    imageArray = [json valueForKeyPath:@"BranchByList.flag_image"];

    [_datad reloadData];
  }

tableview delegate methods are:

-(NSInteger)numberOfSectionsInTableView:(UITableView *) tableView{

    return 1;
}

-(NSInteger)tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section{
    return [sellArray count];
    return [buyArray count];
    return [updataedArray count];
    return [imageArray count];

    }

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"path" forIndexPath:indexPath];
    cell.sellbl.text=[sellArray objectAtIndex:indexPath.row];
    cell.buylbl.text=[buyArray objectAtIndex:indexPath.row];
    cell.updatedlbl.text = [updataedArray objectAtIndex:indexPath.row];
    cell.imglbl.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];
       return cell;
}

-(void)tableView: (UITableView *) tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
  }

Solution

  • You are calling image like,

    cell.imglbl.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]]; 
    

    It is fine if image is available in your project otherwise you need to download it from url which you getting in response like,

      NSData *data = [NSData dataWithContentsOfURL:@"you url"];
      UIImage *img = [UIImage imageWithData:data];
      cell.imglbl.image = img;
    

    or you can use SDWebImage, greate third party library to caching the image.

    hope this will help :)