Reputation: 1184
I have a table view where am listing some set of images from the XML file which is retrieved as URL. I extended this with Collection Views where when user selects the image in table view corresponding image will take to next window displaying some set of images as collection view.
Here i face a problem as when i click any image in table view it displays the same images for all collection views. For better understanding i attached the image below,
In this, the table view shows Image-1,Image-2... and so on. When i click the Image-1 in table view the collection view displays as Image1-1,Image1-2,.. and so on. Again when i click the Image-2 in table view it again shows the same Image-1-1, Image-1-2 ...and so on.
Can anyone tell me how to solve this problem,
The corresponding code are shown below,
The tableView
- (void)viewDidLoad
{
[super viewDidLoad];
xmlParser = [[myXMLParser alloc] loadXMLByURL:@"http://sample.com/Images.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 = @"images";
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 setBackgroundView:tweetImageView];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionViewController *collectViewController = [[MyCollectionViewController alloc] initWithNibName:@"detail" bundle:nil];
[self.navigationController pushViewController:collectViewController animated:YES];
}
The CollectionView
- (void)viewDidLoad
{
[super viewDidLoad];
xmlParser = [[myXMLParser alloc] loadXMLByURL:@"http://sample.com/Images.xml"];
UIImage *currentImage = [[xmlParser tweets] objectAtIndex:0];
customimage.image = currentImage;
UIImage *currentImage1 = [[xmlParser tweets] objectAtIndex:1];
customimage1.image = currentImage1;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [xmlParser.tweets count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionViewCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
UIImage *currentTweet = [[xmlParser tweets] objectAtIndex:indexPath.row];
UIImageView *tweetImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,myCell.frame.size.width , myCell.frame.size.height)];
tweetImageView.image = currentTweet;
[myCell setBackgroundView:tweetImageView];
return myCell;
}
UPDATED
MY XML
<?xml version="1.0" encoding="UTF-8"?>
<Products>
<products id="0">
<img>http://myimage.com/images/logo1.png</img>
</products>
<products id="1">
<img>http://myimage.com/images/Main_pic.png</img>
</products>
<products id="2">
<img>http://myimage.com/images/image1.png</img>
</products>
<products id="3">
<img>http://myimage.com/images/image2.png</img>
<img-subproduct>http://myimage.com/images/image2-1.png</img-subproduct>
<img-subproduct>http://myimage.com/images/image2-2.png</img-subproduct>
</products>
<products id="4">
<img>http://myimage.com/images/image3.png</img>
<img-subproduct>http://myimage.com/images/image3-1.png</img-subproduct>
<img-subproduct>http://myimage.com/images/image3-2.png</img-subproduct>
</products>
</Products>
Upvotes: 1
Views: 2121
Reputation: 2918
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
When you click on any row of the tableview (say Image-2) then above mentioned method gets called.
Inside this method you call ::: pushViewController which pushes the collection view class
After this Collection View gets loaded ::: Now tell me how will the view did load method collection view class will understand that its has to show the images corresponding to Image-2
For this when u call pushviewController you should also pass some argument, depending on which you should fetch its corresponding collection and show them.
Update :
To fetch Image when user clicks on table view:
UIImage *selectedIamge = [[xmlParser tweets] objectAtIndex:indexPath.row];
If user clicked first row then indexpath.row = 0 and inside this array >> [xmlParser tweets] >> the image at zero index is the one dat u need. In this way u can get the image.
Its not necessary that u fetch the image applied on the tableview cell, instead u can fetch the image from the array from which tableview cells are insitialized.
Upvotes: 2
Reputation: 2720
Because you use storyboard, MyCollectionViewCell will always load from stroyboard.
So, change MyCollectionViewCell interface:
@interface MyCollectionViewCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *tweetImageView;
@end
And link the property in stroryboard.
Well, I write that for you.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCollectionViewCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
if (!myCell) {
myCell = [[MyCollectionViewCell alloc] initWithFrame:CGRectMake(0,0,myCell.frame.size.width , myCell.frame.size.height)];
}
[myCell.tweetImageView setImage:[[xmlParser tweets] objectAtIndex:indexPath.row]];
return myCell;
}
And MyCollectionViewCell interface and implementation:
@interface MyCollectionViewCell : UICollectionViewCell
@property (weak, nonatomic) UIImageView *tweetImageView;
@end
@implementation MyCollectionViewCell
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
UIImageView *aImageView = [[UIImageView alloc] initWithFrame:self.bounds];
aImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self addSubview:aImageView];
self.tweetImageView = aImageView;
}
return self;
}
@end
You set currentTweet to [[xmlParser tweets] objectAtIndex:indexPath.row]
?
I think it's nil. Check it.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCollectionViewCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
UIImage *currentTweet = [[xmlParser tweets] objectAtIndex:indexPath.row];
UIImageView *tweetImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,myCell.frame.size.width , myCell.frame.size.height)];
tweetImageView.image = currentTweet;
NSLog(@"%@", tweetImageView.image); // Check tweetImageView.image whether nil or not.
[myCell setBackgroundView:tweetImageView];
return myCell;
}
Upvotes: 2
Reputation: 4500
Change :
UIImageView *tweetImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,cell.frame.size.width , cell.frame.size.height)];
To :
UIImageView *tweetImageView = [[[UIImageView alloc]initWithFrame:CGRectMake(0,0,cell.frame.size.width , cell.frame.size.height)]autorelease];
Upvotes: 1