Obtaining UICollectionViewCell Label text - ios

My Current code for performing the segue to my next view controller is as follows:
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [arrayOfDescriptions count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
[[cell IconImage]setImage:[UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]]];
[[cell IconLabel]setText:[arrayOfDescriptions objectAtIndex:indexPath.item]];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"GroupsHomeSegue" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"GroupsHomeSegue"])
{
//IconImage is the name of the image in GroupsViewController i want to pass through. Based upon what each cell is set at anyways.
//logoImage is the name of the image i want to set in GroupsHomeViewController.
//so basically i want to be able to get IconImage and set it as logoImage in a different view controller
}
}
The problem that i am having, is how do i obtain the value of my text from each selected cell individually so that i can post it as another label in a detailed view controller.
The comment lines within my prepare for segue describe exactly what i am trying to achieve. I simply want to obtain the value of an individual UICollectionViewCell Label.text
This may seem similar to previous passing data through view controller posts, but this is different to anything that i have found due to the fact that in these posts the text values are constant i.e the value of the label.text is set to one thing and are not coming from an array.
I would simply just like to know how to find the label value of an individually selected cell and pass it through to my detailed view controller.

To pass information to the next view controller during a segue, you would use the destinationViewController property on the segue parameter passed to prepareForSegue:sender:. You will have to setup properties on that destination view controller to be able to set the values, of course.
To determine what information the user selected, you have a few options. You can create a property on your view controller to store what the user selected and put that value in the property during collectionView:didSelectItemAtIndexPath: based on the indexPath paramter, or you can use the UICollectionView method indexPathsForSelectedItems to get the index path of the selected items during prepareForSegue:sender:. I tend to do the latter.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"GroupsHomeSegue"])
{
NSIndexPath* indexPath = [[someCollectionView indexPathsForSelectedItems] first];
if(indexPath != nil)
{
NSString* selectedDescription = arrayOfDescriptions[indexPath.item];
NSString* selectedImageName = arrayOfImages[indexPath.item];
// Get the destination view controller (the one that will be shown) from the segue and cast it to the appropriate type. Assuming this should be GroupsHomeViewController, but I'm not entirely sure that's correct since I can't see all your code
GroupsHomeViewController* groupsHomeViewController = segue.destinationViewController;
// Set the appropriate properties (Again, I'm guessing here since I can't see your code)
groupsHomeViewController.logoImage = [UIImage imageNamed: selectedImageName];
}
}
}

In "didSelectRowAtIndexPath" pass the indexPath as "sender" (Please see the below code). And in "performSegueWithIdentifier" you will get the indexPath of selected cell.
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:#"GroupsHomeSegue" sender:indexPath];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"GroupsHomeSegue"])
{
NSIndexPath *indexPath = (NSIndexPath*)sender;
NSString* iconImage = arrayOfImages[indexPath.row];
}
}

Try using this code-
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"GroupsHomeSegue" sender:indexPath];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"GroupsHomeSegue"])
{
UICollectionViewCell *cell = [self.collectionView cellForItemAtindexPath:sender];
NSString *text = cell.IconLabel.text;
UIImage *image = cell.IconImage.image;
GroupsHomeViewController* ghVC = segue.destinationViewController;
groupsHomeViewController.logoImage = image;
}
}

Related

How to pass value from a selected cell in embedded collectionView to another viewController?

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.

How do I pass the dictionary values of result dictionary for another view controller page

- (void)viewDidLoad
{
//json parsing
for (NSDictionary *ResultDictionary in dataDictionary) {
ResultTabObject *ResultcurrenObjet = [[ResultTabObject alloc]initWithDate:[ResultDictionary objectForKey:#"MATCH_DATE"] ATeamName:[ResultDictionary objectForKey:#"COMPETITION_CODE"];
[self.ResultHolderArray addObject:ResultcurrenObjet];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
ResultTabCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
ResultTabObject *ResultcurrenObjet = [self.ResultHolderArray
objectAtIndex:indexPath.row];
cell.lblDate.text=ResultcurrenObjet.Date;
return cell;
}
-(NSMutableArray *)ResultHolderArray{
if(!_ResultHolderArray) _ResultHolderArray = [[NSMutableArray alloc]init];
return _ResultHolderArray;
}
I receive the dictionary values by using above code now I have to pass the ResultDictionary to another viewController
Thanks in Advance
Well, there are several ways of doing this, but an easy solution could be adding a ResultDictionary property on your target view controller and setting it just before the view controller changes.
Of you are creating the target view controller manually then just do it there. If the view controllers are created and connected through Interface Builder you can set the property in the - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender method like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// You can use the segue identifier to make sure that you have the correct segue. If there is only one possible segue from your source view controller you can skip this step.
if ([segue.identifier isEqualToString:#"your_segue_id"]) {
DestinationViewController *vc = [segue destinationViewController];
vc.resultTabObject = someResultTabObject; // get it from your array.
}
}
PS: Looking at your code it occurred to me that you might mean passing the ResultDictionary to the cell rather then an actual view controller. If this is the case then just add a ResultDictionary property to your ResultTabCell class and set it inside - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath.

indexPath has no value in Table View

I have a table view displaying a list of contacts and when you click a cell, a detail view controller pops up with labels showing the contact name, number, fax, and address. My problem is that every time I click a cell, the first value in the plist pops up no matter which cell i click. I found my error in indexPath when NSLog(#"%#",indexPath); returned null everytime. I think the problem is in this method but it looks the same in other classes and works fine.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"showCountyInfo"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSLog(#"%#",indexPath);
TSPBookCoverViewController *destViewController = segue.destinationViewController;
destViewController.countyName = [[self.books objectAtIndex:indexPath.row] objectForKey:#"Name"];//error
destViewController.phoneName = [[self.books objectAtIndex:indexPath.row] objectForKey:#"Phone"];
destViewController.faxName = [[self.books objectAtIndex:indexPath.row] objectForKey:#"Fax"];
destViewController.addressName = [[self.books objectAtIndex:indexPath.row] objectForKey:#"Address"];
}
}
When I enter integers instead of indexPath.row, it works like it should, so the problem is here I just can't find it. Sorry if it's obvious I've tried looking for awhile now!
Edit:
Here is didSelectRowAtIndexPath method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Perform Segue
[self performSegueWithIdentifier:#"showCountyInfo" sender:self];
}
The issue is with the following code:
[tableView deselectRowAtIndexPath:indexPath animated:YES];
You are deselecting the cell inside the didSelectRowAtIndexPath: method. So the indexPathForSelectedRow will always return nil
Best way for you to resolve it is to get the Dictionary from self.books array in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
and then perform the segue and get values from that dictionary in prepareForSegue

Is using tableView:willSelectedRowAtIndexPath: correct way to pass variable?

Hello i am trying to pass variable with segue.
I am getting variable to pass with tableView:willSelectedRowAtIndexPath: is this correct way? If it is not, how should i achieve this? (Note: It is working like this.)
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
selectedCoffeeShop = [coffeeShops objectAtIndex:indexPath.row];
return indexPath;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"coffeeShopDetailSegue"]) {
CoffeeShopDetailViewController *controller = (CoffeeShopDetailViewController *)segue.destinationViewController;
[segue destinationViewController];
controller.coffeeShop = selectedCoffeeShop;
}
}
If your segue is made from the cell itself, then there is no need to implement either willSelectRowAtIndexPath or didSelectRowAtIndexPath. You only need prepareForSegue:sender: since the sender argument will be the cell, and you can use that to get the indexPath you need,
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UITableViewCell *)sender {
if ([segue.identifier isEqualToString:#"coffeeShopDetailSegue"]) {
NSInteger row = [self.tableView indexPathForCell:sender].row;
CoffeeShopDetailViewController *controller = segue.destinationViewController;
controller.coffeeShop = coffeeShops[row];
}
}
That way to do it is absolutely fine.
Another way would be to remove the automatic segue trigger from storyboards and instead implement:
tableView:didSelectRowAtIndexPath: to call performSegueWithIdentifier:sender:.
It could look like this:
- (NSIndexPath *)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedCoffeeShop = [coffeeShops objectAtIndex:indexPath.row];
[self performSegueWithIdentifier:#"coffeeShopDetailSegue" sender:self];
return indexPath;
}
In that case you still need your implementation of prepareForSegue:sender:.
You could also do it completely without segues, using UINavigationController, but then you'd have to instantiate the CoffeeShopDetailViewController programmatically as well.
Your approach is perfectly fine though!
As noted in the comments, you can remove [segue destinationViewController];, since this returns the destination view controller which you already saved in the variable controller in the line right above. :)

Unable to pass unique data during segue

I am having trouble getting the unique data from the selected table cell. Every cell I click seems to pass the same data. Any advise on what I did wrong here?
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"MallToVendor"]) {
MingieAdvertisementIndividualViewController *destViewController = segue.destinationViewController;
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
destViewController.mallName = [[advertisements objectAtIndex:indexPath.row] name];
}
}
Instead of using indexPathForSelectedRow, save the selected indexpath.row in the didSelectRowAtIndexPath method in an integer variable and use it in prepareForSegue.
If you use indexPathForSelectedRow it will always return indexpath of the first row.
Hope this helps!
Define this property in your #implementation file
#property(nonatomic) int indexOfSelectedRow;
In your didSelectROwAtIndexPathmethod do this
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.indexOfSelectedRow=indexPath.row;
}
And in your prepareForSegue method, do this-
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"MallToVendor"])
{
MingieAdvertisementIndividualViewController *destViewController=segue.destinationViewController;
destViewController.mallName = [[advertisements objectAtIndex:self.indexOfSelectedRow] name];
}
}

Resources