I have a UICollectionView within a UITableviewCell that displays Images. All the images are displayed just fine. I want to set it up so that when you tap an image cell it performs a segue which displays the image on a UIViewController. In my prepare for segue method, the index of the image cell is not being set. only the very first cell(index 0) is passed despite clicking on a different image cell. Did some research but all I found was this link..
http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell
it helped me understand that the problem was making sure that the UICollectionView understands that the UIViewController is its delegate and not the UITableViewCell but tha'ts about all I got from it..not sure how to go about it. Any help is greatly appreciated. Thanks in advance
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:#"showPhoto"])
{
NSIndexPath *indexPath = [[self.collectionView indexPathsForSelectedItems] lastObject];
if([segue.destinationViewController respondsToSelector:#selector(setImage:)])
{
_currentItem =[_photoItems objectAtIndex:indexPath];
[segue.destinationViewController performSelector:#selector(setImage:) withObject:_currentItem];
}
}
}
Related
In UIViewController 1, I have set up an array. This UIViewController segues to UIViewController 2.
In UIViewController 2, there is a UITableView with custom UITableViewCell. There's also a UIButton which segues perfectly fine back to UIViewController 1.
In the custom cell, there is a collectionView. This is populated by the array from ViewController 1.
My question is, when an item is selected in the collectionView (UIViewController 2 - custom UITableViewCell class), how to pass that value all the way back to UIViewController 1?
I'm sorry if this is repetitive. I've referred to many similar entries here but nothing seems to be working. I've also tried this:
http://www.appcoda.com/ios-collection-view-tutorial/
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showRecipePhoto"]) {
NSArray *indexPaths = [self.collectionView indexPathsForSelectedItems];
RecipeViewController *destViewController = segue.destinationViewController;
NSIndexPath *indexPath = [indexPaths objectAtIndex:0];
destViewController.recipeImageName = [recipeImages[indexPath.section] objectAtIndex:indexPath.row];
[self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
}
}
I keep getting the null value returned and I'm not sure why.
(I'm not using storyboard. And this is my first attempt at programming of any kind. Would appreciate any input!)
You can use UICollectionViewDelegate.
Add in your class:
class YourViewController: UIViewController, UICollectionViewDelegate { ... }
and use - (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath this event is called when the cell is selected; you must save the value in a property; like that:
- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
selectedRecipeImageName = [recipeImages[indexPath.section] objectAtIndex:indexPath.row];
...
[self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
}
and then:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showRecipePhoto"]) {
RecipeViewController *destViewController = segue.destinationViewController;
destViewController.recipeImageName = selectedRecipeImageName
}
}
You can do it either with delegation or completion block.
To solve with delegation please follow this link.
To solve with completion block please follow this link.
I have an UISearchBar in on top of my TVC. If the search is active it displays another tableView on top of the normal tableView (this is normal). Now i need to get the "searchTableView" in prepareForSegue() because I need to call:
var newIndexPath = table.indexPathForSelectedRow!
and this fails if you search something.
I also can't do the decision in didSelectRowAtIndexPath() because the segue is called to fast because it is 'linked' directly to the UITableViewCell. I also tried to create the segue from the ViewController but this also fails because the segue needs to end on the same ViewController again.
So basically I want to ask if anyone knows how to solve the error in the Code line above will doing a search.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath;
if (self.searchDisplayController.active)
{
indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
}
else
{
indexPath = [self.tableView indexPathForSelectedRow];
}
// Use the indexPath...
}
So this issue is similar to the one described here UITableView - IndexPath always returns 0 first & takes 2 clicks to pass correct data to view controller however the answer they suggested does not help to fix the problem so I'm asking specifically with my code. This is my prepareForSegue method:
//send to a detail version of each of the chatters allowing a report a user
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"pushToDetail"]){
ChatterDetailViewController *chatterDetail = [segue destinationViewController];
UITableViewCell *cell = (UITableViewCell*)sender;
long indexPathSelected = [self.tableView indexPathForCell:cell].row;
Chatter *detailChatter = [_arrayWithoutME objectAtIndex:indexPathSelected];
NSData *fbImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString: detailChatter.url]];
_detailProfile = [UIImage imageWithData:fbImageData];
chatterDetail.detailProfile = _detailProfile;
}
}
Which obviously looks different from the answer suggested on the other question, however I tried that and it did not change anything. Whenever I click on a tableView cell it completes the segue and I see the the profile picture from the first cell (index 0) and not the cell where it was clicked. I tried using
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
however it didn't pass the image value like I needed and also when I tried to return using an unwind segue it said the navigation tree was corrupted (doesn't happen with the current code). Basically I can't seem to find why selecting any cell returns only the first cell's information.
EDIT:
here is some code from Chatter Detail's view did load method:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.hidesBackButton = YES;
_detailProfilePicture.contentMode = UIViewContentModeScaleAspectFill;
_detailProfilePicture.image = _detailProfile;
[_detailProfilePicture sizeToFit];
}
Again, it does return a picture, however it is only the picture from cell at index 0.
There is an easier way to extract the indexPath for your selected row in a UITableView:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"pushToDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
// section and row return an int like this:
NSLog(#"index path: section %i, row %i", indexPath.section, indexPath.row);
}
}
However, your code does give the correct row value as long, and a long can indeed be used to get an object from an NSArray. I would put a breakpoint or log message at the end of this method and see if the correct value is actually in your array.
I would also check what value you get in your indexPathSelected - this should reflect the selected row.
I used two tableviewcontrollers,i want to display country names array data in
first tableviewcontroller (recipeBookTableViewController) and other capital array data in second tableviewcontroller (RecipetableviewController),
when a first tableviewcontroller cell is clicked it should move to second tableviewcontroller with capital array data.
I have taken prepareforSegue method to shift to second tableview. but the method is never called.
how to send data to next tableview.
RecipeBookViewController(table1) Recipetableview(table2)
INDIA (cell) ------------> Delhi(cell)
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"showRecipeDetail"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
RecipeDetailViewController *destViewController = segue.destinationViewController;
destViewController.second = [onew objectAtIndex:indexPath.row];
NSLog(#"%#",onew);
NSLog(#"data is %#",destViewController.second);
// destViewController.lable2 = [derivationArray objectAtIndex:indexPath.row];
// destViewController.image = [iconArray objectAtIndex:indexPath.row];
}
}
Problem is in creating segue.
Make sure you have created segue from cell of first table to second viewController.
Name the segue as showRecipeDetail.
Look at the image below:
I used a UICollectioView to load Images with a NBMutableArray from web and working well.
Now adding a Tap Gesture Recognizer on my UIImageView and i will pass the information to other view like a segue identifier example:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([#"getImage" isEqualToString:segue.identifier]) {
NSIndexPath *index = [self.collectionView indexPathForCell:sender];
[[segue destinationViewController] setLinkImg:(arrayCollectionImages)[index.row][#"image"]];
}
In MyStoryBoard i used this:
if I leave only this function my app go to crash when tap on image.
any body know how can do that?
No need to add Tap Gesture Recognizer to UIImageView. Just connect 'Triggered Segues' of CollectionCell to your destination ViewController. So that sender will be CollectionCell in
*- (void)prepareForSegue:(UIStoryboardSegue )segue sender:(id)sender method.
:)
obviously it will crash.
it will search the method "setLinkImg" on details page where you need to show this.
so make sure this method is active on details page where you are going to show image.
and again check also the collection array should contains any value
so in details page where you need to show image should contain
Code should be there in details page
- (void) setLinkImg:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
your-imageview-object.image = [uiimage imagename:self.detailItem)];
or any url should set on image view object.
}
}
let me inform if any need