get tableView of a UISearchBar - ios

I have an UISearchBar in on top of my TVC. If the search is active it displays another tableView on top of the normal tableView (this is normal). Now i need to get the "searchTableView" in prepareForSegue() because I need to call:
var newIndexPath = table.indexPathForSelectedRow!
and this fails if you search something.
I also can't do the decision in didSelectRowAtIndexPath() because the segue is called to fast because it is 'linked' directly to the UITableViewCell. I also tried to create the segue from the ViewController but this also fails because the segue needs to end on the same ViewController again.
So basically I want to ask if anyone knows how to solve the error in the Code line above will doing a search.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath;
if (self.searchDisplayController.active)
{
indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
}
else
{
indexPath = [self.tableView indexPathForSelectedRow];
}
// Use the indexPath...
}

Related

prepareforSegue method on UITableViewControllers

I used two tableviewcontrollers,i want to display country names array data in
first tableviewcontroller (recipeBookTableViewController) and other capital array data in second tableviewcontroller (RecipetableviewController),
when a first tableviewcontroller cell is clicked it should move to second tableviewcontroller with capital array data.
I have taken prepareforSegue method to shift to second tableview. but the method is never called.
how to send data to next tableview.
RecipeBookViewController(table1) Recipetableview(table2)
INDIA (cell) ------------> Delhi(cell)
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"showRecipeDetail"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
RecipeDetailViewController *destViewController = segue.destinationViewController;
destViewController.second = [onew objectAtIndex:indexPath.row];
NSLog(#"%#",onew);
NSLog(#"data is %#",destViewController.second);
// destViewController.lable2 = [derivationArray objectAtIndex:indexPath.row];
// destViewController.image = [iconArray objectAtIndex:indexPath.row];
}
}
Problem is in creating segue.
Make sure you have created segue from cell of first table to second viewController.
Name the segue as showRecipeDetail.
Look at the image below:

display new UIview when row is selected from UITableView

I have a UITableView which has 3 rows. the first row calls a phone number when pressed, which is working fine.
I have two other UIViewControllers which I would like to be displayed when the respective row is selected in the UITableView.
Below is my didSelectRow method.
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//deselect row
[tableView deselectRowAtIndexPath:indexPath animated:YES];
switch (indexPath.row)
{
case 0:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:1800333000"]];
break;
case 1:
break;
case 2:
break;
default:
break;
}
}
As can be seen, I've set up my switch expression. Im trying to figure out how to call the other two UIViews.
Could someone please provide the code required to display these views?
Your question is not exactly clear about what exactly you are doing but I think what you need is to make a segue and specifically a segue from a UITableViewCell.
First thing to do is control-click and drag a connection from your UITableView cell to the other view in the storyboard. This should create a line between the table view cell and the next view with a little circle. Click on that circle to set and edit the segue properties in the properties inspector. Then in the UITableViewController class you need to have a prepare for segue method. Here is some example code that I've written, its got an extra if statement in there because I actually had 2 UITableViews - one from the UITableViewController and one from the searchbarviewcontroller (which was within the UITableViewController).
// In a story board-based application, you will often want to do a little preparation before navigation
- (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 ([sender isKindOfClass:[UITableViewCell class]]) {
NSIndexPath * SBidxPath = [self.searchDisplayController.searchResultsTableView indexPathForCell:sender];
NSIndexPath * TBidxPath = [self.tableView indexPathForCell:sender];
NSIndexPath * idxPath = [[NSIndexPath alloc] init];
if (!SBidxPath) {
idxPath = TBidxPath;
}
else{
idxPath = SBidxPath;
}
if ([segue.identifier isEqualToString:CELL_PUSH_SEGUE_ID]) {
if ([segue.destinationViewController isKindOfClass:[YTViewController class]]) {
[self prepareYTViewController:segue.destinationViewController withDictionaryEntry:[self.dictionaryEntries objectAtIndex:idxPath.row]];
}
}
}
}
The other thing is that you also need to look into what a NSIndexPath is. Typically the property that you use from that structure is the .row property.

UIcollectionview within UITableviewCell

I have a UICollectionView within a UITableviewCell that displays Images. All the images are displayed just fine. I want to set it up so that when you tap an image cell it performs a segue which displays the image on a UIViewController. In my prepare for segue method, the index of the image cell is not being set. only the very first cell(index 0) is passed despite clicking on a different image cell. Did some research but all I found was this link..
http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell
it helped me understand that the problem was making sure that the UICollectionView understands that the UIViewController is its delegate and not the UITableViewCell but tha'ts about all I got from it..not sure how to go about it. Any help is greatly appreciated. Thanks in advance
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:#"showPhoto"])
{
NSIndexPath *indexPath = [[self.collectionView indexPathsForSelectedItems] lastObject];
if([segue.destinationViewController respondsToSelector:#selector(setImage:)])
{
_currentItem =[_photoItems objectAtIndex:indexPath];
[segue.destinationViewController performSelector:#selector(setImage:) withObject:_currentItem];
}
}
}

How to obtain data passed in performSegueWithIdentifier

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

Setting Specific UITableView rows to push in a specific detail view?

I currently have 7 rows in my UITableView and I'm using segues to push in a detail view. However, I currently have two different view controllers, and want something like this:
If row 1 and 2 are selected push in: "showMenuDetail1"
If row 3 through 4 are selected push in: showMenuDetail2"
This is what I currently have: I'm not sure how to exactly assign rows to a specific one.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showMenuDetail1"]) {
NSIndexPath *indexPath = [self.menuTableView indexPathForSelectedRow];
HomeDetailViewController *HDViewController = segue.destinationViewController;
HDViewController.theName = [[menu objectAtIndex:indexPath.row] name];
}
}
Any help would be appreciated! I know it's an if and else statement but I'm not sure how to write out the exact condition. Thanks!
Create two segues. The important idea is to not attach either to the table view cells, instead drag them both from the view controller containing the table to their respective destinations. Give them each an identifier.
Then in tableView:didSelectRowAtIndexPath:, examine the indexPath.row and trigger the appropriate segue.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row < 2) { // or whatever
[self performSegueWithIdentifier:#"SegueForRows0and1" sender:self];
}
// etc...
}
You can still implement prepareForSegue if you need it, including, if you need it, referring to the tableView's indexPathForSelectedRow.

Resources