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];
}
}
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.
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. :)
My UITableView include multitype data, one is showing some pictures and another is visiting website, so how do I implement?
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDate *object = _objects[indexPath.row];
[[segue destinationViewController] setDetailItem:object];
}
}
Use Following approach
Use following table delegate method,and in this method check your data type is URL or Image according to these push particular View Controller.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// check your data type
if(Data type is URL)
{
From Here Push ViewController for URL
}
if(DATA TYPE IS IMAGE)
{
From HERE push ViewController FOR Image
}
}
OR Another approach :
1, Take one BOOL variable, set its value in following method, if it is URL set BOOL YES and if it is image set BOOL NO.
2, Check BOOL value and handle you subview in your detailViewController by hiding and Show it.
I have two methods :
- (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
testNumber=indexPath.row;
testNumber=testNumber+1;
NSLog(#"Test number : %i",testNumber);
}
then
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:#"testStart1"])
{
tViewController *tvc = [segue destinationViewController];
tvc.testNumberTVC=testNumber;
}
}
I have got also a segue who is triggered by selecting a row in my UITableView.
My problem is when I select a row, prepareForSegue is acting before didSelectRowAtIndexPath so the new value of testNumber is not transferred.
I would like to implement prepareForSegue only when didSelectRowAtIndexPath is done or better:
Transfer the value of testNumber using only didSelectRowAtIndexPath method and so removing prepareForSegue method.
I've seen few topics about transfering data from UITableView to DetailView but with the new xCode 5, when an error message appears, I don't really know if this is because solution is outdated or if there is a real error is my code.
You could create your segues between the view controllers rather than from cell selection and set the identifier of the segue in the storyboard. Then you could do something like this:
- (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
testNumber=indexPath.row;
testNumber=testNumber+1;
NSLog(#"Test number : %i",testNumber);
[self performSegueWithIdentifier:#"mySegueIdentifier" sender:self];
}
Another option would be to check out the answer to this link here. Basically you can get the cell indexPath in prepareForSegue like this:
UITableViewCell *cell = (UITableViewCell*)sender;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
This will probably take two seconds to answer, but my search skills have not gotten me very far on this issue. I am performing a segue but I'm not sure how to grab the id on the destination. Here is my code on the tableview controller.
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath: NSIndexPath *)indexPath{
NSLog(#"reaching accessoryButtonTappedForRowWithIndexPath:");
[self performSegueWithIdentifier:#"leads_calls_to_detail" sender:[[self.leads_calls objectAtIndex:indexPath.row] objectForKey:#"ID"]];
}
What do I have to create on my destination view controller to be able to grab the id that I'm attempting to pass, or is the way I'm performing my segue incompatible with what I am attempting?
You should just pass values to the destinationViewController inside prepareForSegue: and pass self as the sender.. try using something like:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"leads_calls_to_detail"])
{
YourViewController *controller=(YourViewController *)segue.destinationViewController;
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
//Or rather just save the indexPath in a property in your currentViewController when you get the accessoryButtonTappedForRowAtIndexPath callback, and use it here
controller.yourPropertyToSet = [self.leads_calls objectAtIndex:path.row];
}
}
And also according to Rob Mayoff, you can grab the index path for the row that the accessory was tapped at by using something like this:
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];//where sender is the sender passed in from prepareForSegue:
How to find indexPath for tapped accessory button in tableView