UITableView, how to handle when rows are clicked? - ios

I can't seem to find how to display another 'UIViewController' view when one row is clicked. So like when the row is clicked it goes to the next view and displays more information about row.
I think I need to use something like the - (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath delegate. Then my second question is how do I pass the data that was in that row, like the title, to the next view controller?
Thanks

If you are using a storyboard:
.h
#interface YourTableViewController : UIViewController
{
// you just need int selectedRow inside of #interface
int selectedRow;
}
.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// this method is going to give you NSIndexPath *indexPath
selectedRow = indexPath;
[self performSegueWithIdentifier:#"nameOfYourSegue" sender:self];
}
You might already have this method, add the code inside of it if that's the case. This method is called when a segue is about to perform.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"nameOfYourSegue"]){
//Pass data from one controller to the next
[[segue destinationViewController] setRowIndex:selectedRow];
}
}
selectedRow is going to have the index of which row was selected with 0 being the first row.
In your next view controller's class:
.h
- (void)setRowIndex:(int)rowIndex;
.m
- (void)setRowIndex:(int)rowIndex
{
//assign your variable to rowIndex
}
Once you have this working, you can edit this code to pass an array of information.

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
NSString *title = cell.textLabel.text;
Like #rmaddy said, Its better if you do some research, This is very basic, I would recommend you the:
http://www.raywenderlich.com/
Site for great tutorial, they really have every thing you need there.

Here is a hypothetical example:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
MyCustomViewController *vc = [[MyCustomViewController alloc]initWithTitle:selectedCell.textLabel.text];
// Assuming your UITableViewDelegate has a navigationController
[self.navigationController presentViewController:vc animated:YES completion:nil];
}

use the method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
you can get the cell by the index path:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
the code:
- (void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
}
after getting the cell data youc can use it
answer to question 2:
you can connect in the interface builder your cell with the next vc by seque and in the method prepareforseque just pass the data to the
segue.destenationViewController.yourPoperty
now in the viewdidload method:
self.title = self.property

Related

Objective-C prepareForSegue Issue

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.

Using an “IF” statement for popping to two possible View Controllers

Currently I just have a segue hooked up to a UITableView that pops to the next UITableView in the navigation stack.
What I need to do though now instead, is set up an if statement based on the text in the UITableView cell, to decide which of two possible UIViewControllers to pop to.
So ViewController2 needs to be able to pop either":
- back to ViewController1
OR
- forward to ViewController3
based on the text in the UITableView cell that is populated by JSON.
Can you help me out with that? Will post any code needed. Thanks!
Current ViewController:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"standardCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Spring *spring = [springs objectAtIndex:indexPath.section];
Leaf *leaf = [spring.leafs objectAtIndex:indexPath.row];
cell.textLabel.text = leaf.shortName;
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if ([segue.identifier isEqualToString:#"themes"]) {
UITableViewCell *cell = (UITableViewCell*)sender;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
// Get reference to destination view controller
MRTViewController *tvc = (MRTViewController *)[segue destinationViewController];
}
}
UPDATE
Added code per #troops but still not sure how to pop to VC3
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *stringToCheck = #"AAA";
if([cell.textLabel.text isEqualToString:stringToCheck])
{
//Pop to VC1
[self.navigationController popToViewController:[[self.navigationController viewControllers]objectAtIndex:0] animated:YES];
} else {
//Pop to VC3, not sure what to put here
// Pass the info
tvc.spring = [springs objectAtIndex:indexPath.section];
tvc.leaf = [tvc.spring.leafs objectAtIndex:indexPath.row];
tvc.buttonNumber = _buttonNumber;
}
}
You could simply check the cell's textLabel's string: If you really want to use segues and not the tableView's delegate method of: didSelectRowAtIndexPath: That's why I based my answer off what your initial question/code looks like.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *stringToCheck = #"AAA";
if ([cell.textLabel.text isEqualToString:stringToCheck])
{
// Pop to VC1
[self.navigationController popToRootViewControllerAnimated:YES];
}
else
{
// Push to VC3
MRTViewController3 *tvc = [self.storyboard instantiateViewControllerWithIdentifier:#"YourID"];
tvc.spring = [springs objectAtIndex:indexPath.section];
tvc.leaf = [tvc.spring.leafs objectAtIndex:indexPath.row];
tvc.buttonNumber = _buttonNumber;
[self.navigationController pushViewController:tvc animated:YES];
}
}
Use delegate method, get cell, compare text.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if([cell.textLabel.text isEqualToString:#"Your String"])
{
//Pop or present view controller
}
}

Seguing data from one UITableViewController to another UITableViewController via performSelector:withObject:

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;
}
}

Combine didSelectRowAtIndexPath and prepareForSegue

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.

navigational controller not passing data from tableview to viewcontroller

The code for Tableview :
-(void)tableView:(UITableView *)tableView didselectRowAtIndexPath:(NSIndexPath *)indexPath{
ViewControllerDisplayMsg *detail = [self.storyboard instantiateViewControllerWithIdentifier:#"viewController3"];
cellvalue=[array objectAtIndex:indexPath.row];
detail.DisplayMsgString = cellvalue;
[self.navigationController pushViewController:detail animated:YES];
}
Code in viewController3 :
- (void)viewDidLoad
{
[super viewDidLoad];
self.DisplayMsgLabel.text= self.DisplayMsgString;
}
The code above is not passing data its blank when the viewcontroller3 is opened.Why is the data not passing .What code is missing ?
Please help me out.Really appreciate the help.
Thanks in Advance.
You have used
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
}
It is
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
I hope you have typed it by mistake please share your result if it is something else than this
Since I had segue from the story board The didselect method was not being called so -
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// test segue.identifier if needed
ViewControllerDisplayMsg *viewController = segue.destinationViewController;
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
// set properties of viewController based on selection indexPath
cellvalue=[array objectAtIndex:indexPath.row];
NSLog(#"str : %#",cellvalue);
viewController.DisplayMsgtext = cellvalue;
}
This worked perfectly fine.I have to check it if this is right way of implementing but otherwise its working fine now.

Resources