sathya
sathya

Reputation: 384

why images not loading from xml file of url and not showing in table view

Am retrieving XML file from the url and i got the images through parsing it. My XML file consists like this,

<Products>
<products id="1">
<img>http://images.com/images/image1.jpg</img>
</products>
<products id="2">
<img>http://images.com/images/image2.jpg</img>
</products>
<products id="3">
<img>http://images.com/images/image3.jpg</img>
</products>
</Products>

The Tableviewcontroller.m has the code like this,

- (void)viewDidLoad{
 [super viewDidLoad];
 xmlParser = [[XMLParser alloc] loadXMLByURL:@"http://images.com/Products.xml"];
 [self.tableView reloadData];}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [xmlParser.tweets count];}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Products";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];}
UIImage *currentTweet = [[xmlParser tweets] objectAtIndex:1];
cell.imageView.image = currentTweet;
[cell.contentView addSubview:self.productImage];
return cell;}

Here xmlParser is the object of class XMLParser and tweets is the array. The product image is reference of the UIImageview. Now my problem is when i run the app it shows the image in tableview as a thumbnail picture, but i want to show image as the below screenshot i shown.

Desired output

Also am confused in referencing UIImage view in storyboard, when i define productImage as UIImageView in .h file and try to connect the storyboard it shows me error like Illegal Configuration: Connection "productImage" cannot have a prototype object as its destination. so kindly tell me how to connect it in storyboard too.

Kindly suggest me to solve this,

Upvotes: 1

Views: 838

Answers (3)

Swati
Swati

Reputation: 2918

1) Problem : Getting a thumbnail image in table view but you want to show that image in background (complete cell width) Am i right??

Solutions :

In below mentioned way your image can be spread in background

 UIImage *currentTweet = [[xmlParser tweets] objectAtIndex:1];
 UIImageView *tweetImageView = [UIImageView alloc] 
                               initWithFrame:CGRectMake(0,0,
                               cell.frame.size.width , cell.frame.size.height)];
 tweetImageView.image = currentTweet;
 [cell.contentView addSubview:tweetImageView];

Another option is that you can set the background image of the cell like :

[cell setBackgroundView:tweetImageView]

Update :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (cell == nil) 
     {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                      reuseIdentifier:CellIdentifier];
     }
     UIImage *currentTweet = [[xmlParser tweets] objectAtIndex:indexPath.row];
     UIImageView *tweetImageView = [UIImageView alloc] 
                                   initWithFrame:CGRectMake(0,0,
                                   cell.frame.size.width , cell.frame.size.height)];
     tweetImageView.image = currentTweet;
     [cell.contentView addSubview:tweetImageView];
     return cell;
}

Dont create separate imageviews instead use one image view.

And for every row(indexPath.row) pass different image to the imageview.

For every row a new cell gets created an its new imageview will fetch corresponding image from array

Hope it helps :)

Upvotes: 1

Pranav Jaiswal
Pranav Jaiswal

Reputation: 3752

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Products";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];}

UIImage *currentTweet = [[xmlParser tweets] objectAtIndex:indexPath.row];
UIImageView *tweetImageView = [UIImageView alloc] 
                           initWithFrame:CGRectMake(0,0,
                           cell.frame.size.width , cell.frame.size.height)];
tweetImageView.image = currentTweet;
[cell.contentView addSubview:tweetImageView];

return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return self.view.frame.size.height/3;
}

Upvotes: 3

arthankamal
arthankamal

Reputation: 6413

You are trying to assign ID type to UIImage

Try this code to load image for you cell

NSURL *imageURL = [NSURL URLWithString:[[xmlParser tweets] objectAtIndex:1]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *currentTweet = [UIImage imageWithData:imageData];
cell.imageView.image = currentTweet;

Upvotes: 2

Related Questions