didSelectRowAtIndexPath UISearchBar - ios

I have searched answers for this question but I didn't find any relevant answers. In my table view when I touch one cell, a Detail View is pushed. Source code :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"trajetDetail" sender:tableView];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"trajetDetail"]) {
RechercheDetailViewController *detailViewController = segue.destinationViewController;
if (sender == self.searchDisplayController.searchResultsTableView)
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *destinationTitle = [[self.trajets objectAtIndex:[indexPath row]] name];
[detailViewController setTitle:destinationTitle];
}
}
}
The question is that when users make a search with the UISearchBar (and UISearchDisplayController) that they could also touch the cell and be pushed to the detail view.
Sorry for my english and if it's not clear, just tell me I'll try to make it clearer.
Kindest regards

what wrong i see in your code is in your prepareforsegue method, you are selecting indexpath of standard tableView not the search tableview, this below line needs to be changed
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
the prepareForSegue would become something like this after the change
you should use this code instead
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"trajetDetail"]) {
RechercheDetailViewController *detailViewController = segue.destinationViewController;
if (sender == self.searchDisplayController.searchResultsTableView)
{
NSIndexPath *indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
NSString *destinationTitle = [[self.trajets objectAtIndex:[indexPath row]] name];
[detailViewController setTitle:destinationTitle];
}
}
}

Related

How to pass my id click on CollectionViewCell to other ViewController

I have created gallery view in my app. In that add segment controller , first index is table view and second index is collection view. First index (table view) is proper work in my app, But second index (collection view) show array stored pictures but I want to click on collection view cell and move to another view and pass my UID to another view controller. I tried but could not pass my id. Please help. Thank you
Segue method
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"Tableview"]) {
NSIndexPath *indexPath = [self.table_view indexPathForSelectedRow];
Detail_view *destViewController = segue.destinationViewController;
destViewController.uid_value = [id_ary objectAtIndex:indexPath.row];
}
else if ([segue.identifier isEqualToString:#"Collectionview"])
{
NSArray *indexPaths = [self.collection_view indexPathsForSelectedItems];
collection *destViewController = segue.destinationViewController;
NSIndexPath *indexPath = [indexPaths objectAtIndex:0];
destViewController.uid_value = [id_ary[indexPath.section] objectAtIndex:indexPath.row];
[self.collection_view deselectItemAtIndexPath:indexPath animated:NO];
}
}
I think you need to improve your code rather then doing split arrays better to user NSObject class manage the array with Object Class for your solution use below code
else if ([segue.identifier isEqualToString:#"Collectionview"])
{
NSArray *indexPaths = [self.collection_view indexPathsForSelectedItems];
collection *destViewController = segue.destinationViewController;
NSIndexPath *indexPath = [indexPaths firstObject]; // you can also use firstObject rather then ObjectAtIndex:0
destViewController.uid_value = [id_ary objectAtIndex:indexPath.row];
[self.collection_view deselectItemAtIndexPath:indexPath animated:NO];
}
try this-
take a global indexPath for ex. NSIndexPath *selectedIndexPath; Now add this line in didSelectItemAtIndexPath i.e.
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
selectedIndexPath=indexPath;
[self performSegueWithIdentifier:#"Collectionview" sender:self];
}
now in your prepareForSegue method-
else if ([segue.identifier isEqualToString:#"Collectionview"])
{
collection *destViewController = segue.destinationViewController;
destViewController.uid_value = [id_ary objectAtIndex:selectedIndexPath.row];
// or destViewController.uid_value = [id_ary[selectedIndexPath.section] objectAtIndex:selectedIndexPath.row];
[self.collection_view deselectItemAtIndexPath:selectedIndexPath animated:NO];
}
hope this will solve your issue..:)

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*)... ) ...

Getting same records from UITableView on Detail Button click in iOS

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

Segue not passing correct indexpath of row always section 0

I'm used sectioned tableView.If I click tableview always it passing indexpath 0 to detail view controller.If I click second row but it indexpath pass always pass 0.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
if ([segue.identifier isEqualToString:#"toNext"]) {
detailTableViewController *detailVC = [segue destinationViewController];
[detailVC setkMessageDict:(NSDictionary*)[nArray objectAtIndex:[self.mytableView indexPathForSelectedRow].section]];
}
What's wrong in code?
You need to create index-path object in to your seque like:-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self performSegueWithIdentifier:#"toNext" sender:indexPath];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
NSIndexPath *indexPath = [self.tableview indexPathForSelectedRow];
NSLog(#" indexPath row %d",indexPath.row);
NSLog(#" indexPath row %d",indexPath.section);
detailTableViewController *detailVC = [segue destinationViewController];
[detailVC setJobDetailDict:(NSDictionary*)[nArray objectAtIndex:indexPath.row];
}
Yes Ramdy, is right You need to mention row instead of section.
You use this....
[detailVC setJobDetailDict:(NSDictionary*)[nArray objectAtIndex:[self.mytableView indexPathForSelectedRow].row]];
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
if ([segue.identifier isEqualToString:#"toNext"]) {
NSIndexPath *selectedIndex= [self.mytableView indexPathForSelectedRow];
NSDictionary *myDic=(NSDictionary*)[nArray objectAtIndex:indexpath.section];
detailTableViewController *detailVC = [segue destinationViewController];
[detailVC setkMessageDict:(NSDictionary*)[[myDic objectForKey:#"locations"] objectAtIndex:selectedIndex.row]];
}
Please check your Array having the proper data from your section tableview otherwise add the NSDictionary value to array and pass it to detailTableViewController then try.Problem is your are not passing indexpath of section here.Hope its helpful for you.
Try updating the selected row indexPath using API:
selectRowAtIndexPath:animated:scrollPosition:
in the following UITableViewDelegate API:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * currentRow = [NSString stringWithFormat:#"%ld",(long)indexPath.row]; // Declare this row variable in .h file
NSString * currentSection = [NSString stringWithFormat:#"%ld",(long)indexPath.section]; // Declare this section variable in .h file
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
if ([segue.identifier isEqualToString:#"toNext"]) {
detailTableViewController *detailVC = [segue destinationViewController];
detailVC.PassRow = currentRow;
detailVC.PassSection = currentSection;
}
The closest answer to the question is Nitin Gohel. But if you have connected the UITableViewCell to the detailTableViewController in the Storyboard, his method is also wrong.
In that case, the prepareForSegue method is provided with the correct UITableViewCell as a parameter stored in the sender parameter. This is entirely handled by UITableView. So the correct code would be:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([sender isKindOfClass:[UITableViewCell class]] && [segue.identifier isEqualToString:#"toNext"])
{
NSIndexPath *indexPath = [self.mytableView indexPathForCell:sender];
detailTableViewController *detailVC = [segue destinationViewController];
[detailVC setJobDetailDict:(NSDictionary*)[nArray objectAtIndex:indexPath.row];
}
}
Do not forget to check for correct segue name.

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

Resources