Combine didSelectRowAtIndexPath and prepareForSegue - ios

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.

Related

IOS/Objective-C: Passing data (array) and receiving detailed strings

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

Issue to choose a cell in a section of a TableView

I have a UITableViewController with three sections and a variable number of cells for each one and when I tap on a cell, I have a detail view.
I use a segue to send the information to my other view. But I saw that if I the for example the first cell of the second section I will have the first cell of my first section. I tried to fix that by indicating the section and the cell but my program doesn't recognize indexPath.section.
Here is my code of the segue :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue isKindOfClass:[TLAnimatedSegue class]]) {
((TLAnimatedSegue *) segue).delegate = self;
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
TVDetailsViewController *destViewController = segue.destinationViewController;
destViewController.details = [[_scheduleList objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
}
}
I don't understand why I have en error with that because when I po my indexPath, I have two element inside. With the breakpoint, this line seems to be the reason of the crash :
destViewController.details = [[_scheduleList objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
Thanks for your help.
Thank you #zcui93 and #Rohit KP, you open my mind and eyes so here is the solution to tackle this little issue and now it works :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue isKindOfClass:[TLAnimatedSegue class]]) {
((TLAnimatedSegue *) segue).delegate = self;
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
TVDetailsViewController *destViewController = segue.destinationViewController;
if(indexPath.section == 0) {
destViewController.details = [_pastScheduleList objectAtIndex:indexPath.row];
}
if(indexPath.section == 1) {
destViewController.details = [_currentScheduleList objectAtIndex:indexPath.row];
}
if(indexPath.section == 2) {
destViewController.details = [_futurScheduleList objectAtIndex:indexPath.row];
}
}
}
Use didSelectRowAtIndexPath method and get cell content using as describe below and perform segue
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
....
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
String id = cell.textLabel.text;
/*... perform segue operation... */
....
}
/* ... for( Customcell *cell = (CustomCell*)... ) ...

UITableView, how to handle when rows are clicked?

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

UITableView Accessory Button Segue Not working

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

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

Resources