I'm trying to pass data (array) with PrepareForSegue into a detailed VC. The destination VC has a label that receives a string. I thought I could transform the array into a string, and the rest was straightforward. This was achieved, though, but kind of wrongly, because, obviously, the text I get, regardless the row I click, returns me the whole object...And my goal is to get details of a specific row...Can someone help me? Guess this is basic stuff, but this is my first app...The method PrepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"detail"]) {
NSString *places = [_spots componentsJoinedByString:#" "];
DetailViewController * destination = segue.destinationViewController;
destination.detailedText = places;
}
EDIT:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSManagedObject *ls = [self.spots objectAtIndex:indexPath.row];
NSLog(#"Table Spots are: %#", ls);
[cell.textLabel setText:[NSString stringWithFormat:#"%#", [ls valueForKey:#"name"]]];
cell.imageView.image = [UIImage imageNamed:#"city.jpg"];
return cell;
}
As far as I understood, you want to performSegue() on a row selection and you want to pass a data specific to the selected row. In order to do this you can use the tableView.indexPathForSelectedRows() method, which is going to return an array with the indexPaths of all selected rows. Then you can get the UITableViewCells for this indexPaths and get their data.
You can pass the string value to the perforSegue method as the sender:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"detail" sender:[_spots objectAtIndex:indexPath.row]];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"detail"]) {
DetailViewController * destination = segue.destinationViewController;
destination.detailedText = sender;
}
}
Related
I am newbie to iOS and learning things in iOS. I am making a demo of UITableView and Segue. I have successfully implemented table-view and now I want to pass the value to another View Controller using segue. I have tried as below, but my problem is I am getting same record every time on detail button clicked from UITableView. I don't have any idea what is wrong in my code.
Can anybody help me to figure it out?
I am posting my code here:
code
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
//NSDictionary *dict = [myArray objectAtIndex:indexPath.row];
// NSString *name = [dict valueForKey:#"name"];
[self performSegueWithIdentifier:#"detail" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"detail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
//NSLog(#"selected index path==%#",indexPath);
DetailView *destViewController = segue.destinationViewController;
destViewController.details = [myArray objectAtIndex:indexPath.row];
}
}
Change your code as below, you don't need to send self as the sender. Send indexPath
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
[self performSegueWithIdentifier:#"detail" sender:indexPath];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"detail"]) {
NSIndexPath *indexPath = sender;
DetailView *destViewController = segue.destinationViewController;
destViewController.details = [myArray objectAtIndex:indexPath.row];
}
}
I am using the prepareForSegue with my UISplitViewController and I have a NSMutableDictionary populating the UITableView with sections. What I am trying to do is pass the value of the selected value in the selected section to the method in my detailViewController, this is what I got so far:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *strPOIndex = [self.tableData[indexPath.row] valueForKey:#"Vendor"];
LHContactsDetail *controller = (LHContactsDetail *)[[segue destinationViewController] topViewController];
[controller setDetailItem:strPOIndex];
controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
controller.navigationItem.leftItemsSupplementBackButton = YES;
}
}
but the issue with this, it returns the numbered item of the first section. Example, if I click on section 2 item 1, it returns section 1 item 1, if I click on section 3 item 2, it returns section 1 item 2. I hope this makes sense. THis is how I am populating my tableview:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSString *sectionTitle = [contactSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionContacts = [contactDirectoryFinal objectForKey:sectionTitle];
NSString *contacts = [[sectionContacts objectAtIndex:indexPath.row] valueForKey:#"Vendor_Name"];
cell.textLabel.text = contacts;
return cell;
}
How can I get the correct value to send to my detailController method ?
I wonder why your data source is different in cellforrow and prepare for segue.
If you manage the same way as sections and rows.
You need to get data from datasource firstly by indexpath.section and then indexpath.row change this in prepareforsegue method.
I'm having big problems learning how to pass data from the cell accessory button to the detail view controller with segues. If I tap the cell the data is passed to the detail view controller in a NSString *selectedItem; and the label on the other view updates with the word that was in the cell -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"toDetail" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"toDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DrugDetailViewController *destViewController = segue.destinationViewController;
destViewController.selectedItem = [[candyArray objectAtIndex:[indexPath row]] name];
}
}
And i connect a segue on the storyboard from the cell to the detail view with a name "toDeatil" It works when i click the cell it goes to the detail screen, but if i put
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"toDetail" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"toDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DrugDetailViewController *destViewController = segue.destinationViewController;
destViewController.selectedItem = [[candyArray objectAtIndex:[indexPath row]] name];
}
}
Almost exactly the same but from the accessorybutton and I connect a segue on the storyboard from the cell accessorybutton to the detail view with a name "toDetail" when I click the cell it goes to the detail screen, but the information is not there, the label dosent get filled out or just uses the first cell detail all the time.
I've tried heaps of combinations. I've tried making my own accessory button, a UIButton in a custom cell but it just uses the first cells details all the time.
The goal is to get didSelectRowAtIndexPath going to one view and accessoryButtonTappedForRowWithIndexPath going to a different view but I need to tell accessoryButtonTappedForRowWithIndexPath what cell was pressed.
Hope this makes sence.
Thank you
You can pass the indexPath as sender to the performSegueWithIdentifier call, like this:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"toDetail" sender:indexPath];//IndexPath as sender
}
And in your prepareForSegue method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"toDetail"]) {
//Detect sender class and act accordingly
NSIndexPath *indexPath = [sender isKindOfClass:[NSIndexPath class]] ? (NSIndexPath*)sender : [self.tableView indexPathForSelectedRow];
DrugDetailViewController *destViewController = segue.destinationViewController;
destViewController.selectedItem = [[candyArray objectAtIndex:[indexPath row]] name];
}
}
You don't need to implement accessoryButtonTappedForRowWithIndexPath.
When prepareForSegue is fired from an accessory view, the sender:(id)sender parameter is set to the UITableViewCell which contains the accessory view. You can then ask the table view for the index of that cell (not the selected index).
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"toDetail"] && [sender isKindOfClass:[UITableViewCell class]]) {
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
DrugDetailViewController *destViewController = segue.destinationViewController;
destViewController.selectedItem = [[candyArray objectAtIndex:[indexPath row]] name];
}
}
That is because [self.tableView indexPathForSelectedRow] will return a NSIndexPath only if the ROW is tapped, not when the accessory button is tapped.
In accessoryButtonTappedForRowWithIndexPath, you will need to pass indexPath.row to the prepareForSegue method manually. Try this:
NSInteger selectedRow;
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
selectedRow = indexPath.row;
[self performSegueWithIdentifier:#"toDetail" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"toDetail"])
{
DrugDetailViewController *destViewController = segue.destinationViewController;
destViewController.selectedItem = [[candyArray objectAtIndex:selectedRow] name];
}
}
What about, if you drive state of what is selected on application delegate! Answer of Raul Juarez do the work, but the target of sender parameter is other!
I have an app that use UITableView, and i need to know what item was touched on accessory button in above screen!
To manage this scenery, i have one property on application delegate, that i update when i touch table view accessory button; so in the next screen, i can retrieve the item touched reading the property of application delegate.
In general, i use de application delegate to code model application (dividing necessary class), so views instances only interact with this model to update necessary states! what i do is focus view work independent of model work!
Maybe some like this:
// Property on Application Delegate
#propety (atomic,readwrite) NSIndexPath* indexTapped;
// Method on UITableViewDelegate instance
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
UIMyAppDelegate* model = (UIMyAppDelegate*)[[UIApplication shared] delegate];
model.indexTapped = indexPath;
[self performSegueWithIdentifier:#"toDetail" sender:self];
}
// Method on another View Controller
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
UIMyAppDelegate* model = (UIMyAppDelegate*)[[UIApplication shared] delegate];
if ([segue.identifier isEqualToString:#"toDetail"]) {
NSIndexPath *indexTapped = model.indexSelected;
DrugDetailViewController *destViewController = segue.destinationViewController;
destViewController.selectedItem = [[candyArray objectAtIndex:[indexPath row]] name];
}
}
So what I am trying to do is I have an NSMutableArray of data I need to pass to another UITableViewController. This NSMutableArray is an array of NSDictionaries that contain the information I want to be displayed in the title of each table view cell. Here is my code before I segue.
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath* indexPath = [self.tableView indexPathForCell:sender];
if ([segue.identifier isEqualToString:#"Title Query"]) {
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
NSString* cellText = cell.textLabel.text;
NSMutableArray* photosToBeShown = [self titleQuery:cellText];
if ([segue.destinationViewController respondsToSelector:#selector(setPhotoTitles:)]) {
[segue.destinationViewController performSelector:#selector(setPhotoTitles:) withObject: photosToBeShown];
NSLog(#"%#", photosToBeShown);
}
}
}
The method setPhotoTitles: that is called by the performSelector: withObject: is the setter of the property (NSMutableArray* ) photoTitles on the UITableViewController that I am seguing to because I wanted to collect the array so I could then call the methods below to set the titles of my table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Photo Title Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = [self titleForRow:indexPath.row];
return cell;
}
- (NSString *) titleForRow: (NSUInteger) row
{
return self.photoTitles[row];
}
What happens when I run this code is I end up in an infinite loop with calls to my setter method (setPhotoTitles:). Now my question is what is the right conceptual way to get around this problem or how can I implement it this way without ending up in an infinite loop. I have all the information I need in my array but I need to pass the array to the new controller but also be able to use the UITableViewCell method to set the rows titles.
In the prepareForSegue: method, rather than overriding setPhotoTitles:, you should create a NSArray property in the destination view controller, as pass the photoTitles array to the NSArray property of the destination view controller. So your prepareForSegue method would look like this:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath* indexPath = [self.tableView indexPathForCell:sender];
if ([segue.identifier isEqualToString:#"Title Query"]) {
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
NSString* cellText = cell.textLabel.text;
NSMutableArray* photosToBeShown = [self titleQuery:cellText];
YourCustomViewController *customViewController = segue.destinationViewController;
customViewController.photosArrayProperty = photosToBeShown;
}
}
I have the following problem: The segue should only be performed if the UITableViewCellSelectionStyleNone is true.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.selectionStyle == UITableViewCellSelectionStyleNone) {
[self performSegueWithIdentifier:#"Update" sender:indexPath];{
}
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"Update"]) {
NSManagedObject *selected = [self.travelA objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
TravelVC *destViewController = segue.destinationViewController;
destViewController.travel = selected;
destViewController.positionString = _positionStringA;
}
}
But this doesn't work. The segue will also be performed if the cell selection style is not UITableViewCellSelectionStyleNone. Maybe there's a way to combine both methods.
Thanks in advance.
You shouldn't be basing your logic on the cell setting of UITableViewCellSelectionStyleNone. Instead, you should be using the indexPath to interrogate your model and find out if selection is available / sensible for that item.