I use predicate to filter a table.
It works very well and also to display the detailView for each cell.
But after a filtering it does not work to display the filtered list in the detailView.
The cell just turns blue when pressing it but does not shift to detailView.
What am I missing here?
The code looks like this.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
Person *person;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
person = [self.filteredPeople objectAtIndex:indexPath.row];
}
else
{
person = [self.people objectAtIndex:indexPath.row];
}
dvController.title = person.firstName;
dvController.price = person.sjuName;
dvController.link = person.sexName;
dvController.picture = [UIImage imageNamed:person.femName];
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}
Here both the tableView are of different class so the can't be equal thus change your if condition as
if ([tableView class] != [UITableView class]) // if it's is of type UISearchResultsTableView
{
person = [self.filteredPeople objectAtIndex:indexPath.row];
}
else
{
person = [self.people objectAtIndex:indexPath.row];
}
Related
I'm trying to link cells from an UITableViewController to UIViewController, but each cell has to call a different UIViewController (so I imagine that I'll have to create one link to each view controller) and I don't know how to link them through storyboard.
This is what I have until now:
MainView.h
#interface MainView : UITableViewController <UITableViewDelegate, UITableViewDataSource>
{
NSArray *tableData;
}
#property (nonatomic, retain) NSArray *tableData;
#end
MainView.m
- (void)viewDidLoad
{
tableData = [[NSArray alloc] initWithObjects:#"1", #"2", #"3", #"4", nil];
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"MyCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MyCell"];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UIViewController *anotherVC = nil;
if (indexPath.section == 0) //here I've also tried to use switch, case 0, etc
{
if (indexPath.row == 0)
{
anotherVC = [[ViewForFirstRow alloc] init];
}
NSLog(#"anotherVC %#", anotherVC); // shows that anotherVC = ViewForFirstRow
[anotherVC setModalPresentationStyle:UIModalPresentationFormSheet];
[anotherVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self.navigationController pushViewController:anotherVC animated:YES];
}
}
#end
So, when first row is selected and I didn't link UITableViewController to UIViewController (ViewForFirstRow), it shows a black screen.
If I link UITableViewController to ViewForFirstRow, it opens the view but without the effect I used in code [self.navigationController pushViewController:anotherVC animated:YES];, so it appears that it called the view due to the link and not because of the code [self.navigationController pushViewController:anotherVC animated:YES];
How can I call the view by code properly and what kind of link should I use in storyboard for that? Also, how can I link the UITableViewController to multiples UIViewController (ViewForFirstRow, ViewForSecondRow, ViewForThirdRow, etc).
Thanks!
First, you can't hook up segues from cell (in a dynamic table view) in the storyboard, you have to do it in code. The code should look like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.row == 0) {
UIViewController *anotherVC = [self.storyboard instantiateViewControllerWithIdentifier:#"anotherVC"];
[self.navigationController pushViewController:anotherVC animated:YES];
}else if (indexPath.row == 1) {
UIViewController *someOtherVC = [self.storyboard instantiateViewControllerWithIdentifier:#"someOtherVC"];
[self.navigationController pushViewController:someOtherVC animated:YES];
}
}
You shouldn't be doing anything with indexPath.section since your table view only has one section.
In storyboard presenting another viewcontroller in modally manner then use this:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
Synergy *objSynergy = (Synergy *)[mainStoryboard instantiateViewControllerWithIdentifier:#"Synergy"];
[self presentModalViewController:objSynergy animated:YES];
Synergy is my controller name You can put here your controller name that you want to present.
Make changes with your requirement. And for different viewcontroller open from different row
Use this:
if (indexPath.section==0)
{
if (indexPath.row ==0)
{
}
}
I am having some issues with pulling up specific ViewControllers depending on which row is selected in my UITableView. I have a table, which contains 3 sections, with 4 rows in each section.
When a row is selected I have it set to call a new view for each row, so 12 views in total to be called depending on which row is selected. The issue is, that only the first 4 views are called, when I tap row number 5, it displays a new view, but it pulls up the first view.
I have set my UITableView up using the following code;
carbData = [[NSArray alloc] initWithObjects:#"What Are Carbs?",#"Why Do I Need Carbs?",#"Simple Vs. Complex",#"Fun Five Facts",nil];
proteinData = [[NSArray alloc] initWithObjects:#"What is Protein?",#"Why Do I Need Protein?",#"Complete Vs. Incomplete",#"Fun Five Facts",nil];
fatData = [[NSArray alloc] initWithObjects:#"What Are Fats?",#"Why Do I Need Fats?",#"Saturated Vs. Unsaturated",#"Fun Five Facts",nil];
[self setTitle:#"Nutrition Table"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.carbData count];
return [self.proteinData count];
return [self.fatData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SimpleTableIdentifier] autorelease];
}
if(indexPath.section == 0)
cell.text = [carbData objectAtIndex:indexPath.row];
if(indexPath.section == 1)
cell.text = [proteinData objectAtIndex:indexPath.row];
if (indexPath.section == 2)
cell.text = [fatData objectAtIndex:indexPath.row];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if(section == 0)
return #"Carbohydrates";
if (section ==1)
return #"Protein";
else if (section ==2)
return #"Fats";
}
I then use the following code to determine which view is to be called, when a certain row is selected;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([[carbData objectAtIndex:indexPath.row] isEqual:#"What Are Carbs?"]) {
CarbsView1 *controller = [[CarbsView1 alloc] initWithNibName:#"CarbsView1" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
if ([[carbData objectAtIndex:indexPath.row] isEqual:#"Why Do I Need Carbs?"]) {
CarbsView2 *controller = [[CarbsView2 alloc] initWithNibName:#"CarbsView2" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
if ([[carbData objectAtIndex:indexPath.row] isEqual:#"Simple Vs. Complex"]) {
CarbsView3 *controller = [[CarbsView3 alloc] initWithNibName:#"CarbsView3" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
if ([[carbData objectAtIndex:indexPath.row] isEqual:#"Fun Five Facts"]) {
CarbsView4 *controller = [[CarbsView4 alloc] initWithNibName:#"CarbsView4" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
if ([[proteinData objectAtIndex:indexPath.row] isEqual:#"What is Protein?"]) {
ProteinView1 *controller = [[CarbsView4 alloc] initWithNibName:#"ProteinView1" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
The app compiles and builds fine, everything works as i'd like it too, however when looking at the titles used, when I try and select "What is Protein?" is pulls up the view for "what are Carbs?" which is in the first if statement.
Any help would be massively appreciated
"What is Protein?" is in section 1, row 0. "What are Carbs?" is in section 0, row 0. Notice that your didSelectRowAtIndexPath method, you don't check the section number anywhere. So when you tap section 1, row 0, your first if statement returns true. I think you want something more like:
if (indexPath.section == 0) {
if(indexPath.row == 0) {
//present appropriate view controller
} else if (indexPath.row == 1) {
} else if (indexPath.row == 2) {
} ...
} else if (indexPath.section == 1) {
if (indexPath.row == 0) {
} else if (indexPath.row == 1) {
} ...
} ...
I followed the tutorial here and was wondering how to make the table appear grouped.
ex:
group1 contains Subview One and Subview Two
group2 contains Subview Three
I switched the type in interface builder but that only shows one group.
Thanks,
Adam
Sidenote* I am a completely new at objective c, hence the tutorial.
EDIT
I thought it might be helpful to put up the piece of code
#import "RootViewController.h"
#import "SubViewOneController.h"
#import "SubViewTwoController.h"
#implementation RootViewController
#pragma mark -
#pragma mark View lifecycle
-(void)awakeFromNib {
views = [[NSMutableArray alloc] init];
SubViewOneController *subViewOneController = [[SubViewOneController alloc] init];
SubViewTwoController *subViewTwoController = [[SubViewTwoController alloc] init];
//Subview 1
subViewOneController.title = #"Subview One";
[views addObject:[NSDictionary dictionaryWithObjectsAndKeys:
#"Subview One", #"title",
subViewOneController, #"controller",
nil]];
[subViewOneController release];
//Subview 2
subViewOneController = [[SubViewOneController alloc] init];
subViewOneController.title = #"Subview Two";
[views addObject:[NSDictionary dictionaryWithObjectsAndKeys:
#"Subview Two", #"title",
subViewTwoController, #"controller",
nil]];
[subViewOneController release];
//Subview 3
subViewOneController = [[SubViewOneController alloc] init];
subViewOneController.title = #"Subview Three";
[views addObject:[NSDictionary dictionaryWithObjectsAndKeys:
#"Subview Three", #"title",
subViewOneController, #"controller",
nil]];
[subViewOneController release];
UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
temporaryBarButtonItem.title = #"Back";
self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
[temporaryBarButtonItem release];
self.title = #"Basic Navigation";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [views count];
}
//
//I think it goes somewhere in here
//
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewStyleGrouped reuseIdentifier:CellIdentifier];
}
cell.text = [[views objectAtIndex:indexPath.row] objectForKey:#"title"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *targetViewController = [[views objectAtIndex:indexPath.row] objectForKey:#"controller"];
[[self navigationController] pushViewController:targetViewController animated:YES];
}
- (void)dealloc {
[views dealloc];
[super dealloc];
}
#end
Ok so you are correct it does go into your cellForRowAtIndexPath: you will be looking to do something like:
if ([indexPath section] == 0) {
//stuff for first section.
}
if ([indexPath section] == 1) {
//stuff for second section.
}
You will need to also deal with how your are configuring the cell from your array. The sections row numbers start with 0. The same thing with didSelectRowAtIndexPath:
If you don't deal with the section where you set the cell.text you will end up with
Subview One
Subview Two
Subview One
I recommend getting the iPhone Programming (The Big Nerd Ranch Guide)
In Interface Builder, choose your UITableView object and choose Grouped as the style.
If you are programmatically creating it, use the initWithStyle: UITableViewStyleGrouped.
EDIT:
if (section == 0) { /* your first section */ }
if (section == 1) { /* your second section */ }
This is all controlled by your UITableViewDataSource. The numberOfSectionsInTableView: method controls how many groups there are, and tableView:numberOfRowsInSection: controls how many rows are in each group.
Your tableView:cellForRowAtIndexPath: on the UITableViewDelegate gets an NSIndexPath object; indexPath.section tells you which group and indexPath.row tells you which row in the group. The cell you return really has no idea which group it is in or which row in the group it is, it's all controlled by the fact that you return it for a particular indexPath when tableView:cellForRowAtIndexPath: is called.
How do make the individual cells go to different View Controller. Right now, using the method im using, if i click the first cell, it takes me to a view controller where i have a pic. now if i go back and click the second cell, it takes me to the same view controller. How do i change this so that all the other individual cells go to individual view controller? what do i add or what do i configure?
here my code:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
NSUInteger row = [indexPath row];
cell.textLabel.text = [breakdownArray objectAtIndex:row];
bookDetailViewController.title = [NSString stringWithFormat:#"%#" , [breakdownArray objectAtIndex:row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
NSInteger row = [indexPath row];
if (self.bookDetailViewController == nil); {
BookDetailViewController *aBookDetail = [[BookDetailViewController alloc] initWithNibName:#"BookDetailView" bundle:nil];
self.bookDetailViewController = aBookDetail;
[aBookDetail release];
}
bookDetailViewController.title = [NSString stringWithFormat:#"%#" , [breakdownArray objectAtIndex:row]];
Books2010AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.breakdownNavController pushViewController:bookDetailViewController animated:YES];
}
One fairly easy way is in your tableView:didSelectRowAtIndexPath: method. You can check the index path parameter and execute different code paths. Something like:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath {
switch(indexPath.row) {
case 1:
[self.navigationController pushViewController:someViewController animated:YES];
break;
case 2:
[self.navigationController pushViewController:someOtherViewController animated:YES];
break;
}
}
Like most design choices, this is just an example of one possible approach. That's the best I can do in this general case.
I would create a new view controller each time instead of using the same one and just changing its properties, more like the example that you have commented out in tableView:didSelectRowAtIndexPath:. This will only work if you don't access your controller from anywhere other than these methods.
First: Remove bookDetailViewController.title = [NSString stringWithFormat:#"%#" , [breakdownArray objectAtIndex:row]]; from your tableView:cellForRowAtIndexPath: method (you should do this anyway, since it is just taking more processor time)
Second: Remove the bookDetailController property from your class. You won't need it anymore.
Third: Implement tableView:didSelectRowAtIndexPath: as follows:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path {
BookDetailViewController *bookDetailViewController = [[BookDetailViewController alloc] initWithNibName:#"BookDetailView" bundle:nil];
NSInteger row = path.row;
bookDetailViewController.title = [NSString stringWithFormat:#"%#" , [breakdownArray objectAtIndex:row]];
//Any other setup on view controller
Books2010AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.breakdownNavController pushViewController:bookDetailViewController animated:YES];
[bookDetailViewController release];
}
I am new to iPhone development, so please pardon my ignorance.
I call a UIViewController from a UIViewController. The first UIViewController is a list of items, and the second is the detail for each of the items.
ListViewController (first UIViewController):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// initialize a AddViewController
selItem = indexPath.row;
selIndexPath = indexPath;
AddViewController *controller = [[AddViewController alloc] init];
// give controller the data to display
// show the AddViewController
[controller setData:[list objectAtIndex:indexPath.row]];
controller.delegate = self;
[self presentModalViewController:controller animated:YES];
[controller release]; // release the controller AddViewController
} // end method tableView:didSelectRowAtIndexPath:
In the 'AddViewController' I have a segment control as both the left and right button bars. On the left, I have 'Exit' and 'Delete'. On the right I have 'Add' and 'Save'. The outlets that the segment controls are linked to are:
AddViewController (second UIViewController):
- (IBAction)delExitSegment:sender
{
// The segmented control was clicked, handle it here
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
switch (segmentedControl.selectedSegmentIndex) {
case 0: // Exit Button
[delegate addViewControllerDidExit:self];
break;
case 1: // Delete Button
[delegate addViewControllerDelItem:self];
break;
}
}
- (IBAction)segmentAction:sender
{
// The segmented control was clicked, handle it here
UISegmentedControl *segCtl = (UISegmentedControl *)sender;
switch (segCtl.selectedSegmentIndex) {
case 0: // Add Button
if (currentCell != nil)
[data setValue:currentCell.textField.text forKey:currentCell.label.text];
[delegate addViewControllerDidFinishAdding:self];
break;
case 1: // Save Button
[delegate addViewControllerUpdate:self];
break;
}
}
When I come back to my first view controller, I have:
ListViewController:
- (void)addViewControllerDidFinishAdding:(AddViewController *)controller
{
NSDictionary *item = [controller values];
if (item != nil)
{
[list addObject:item];
}
[self dismissModalViewControllerAnimated:YES];
[list writeToFile:itemFilePath atomically:NO];
[self calcTotal];
[self.tableView reloadData];
}
- (void)addViewControllerUpdate:(AddViewController *)controller
{
NSDictionary *item = [controller values];
if (item != nil)
{
[list replaceObjectAtIndex:selItem withObject:item];
}
[self dismissModalViewControllerAnimated:YES];
[list writeToFile:itemFilePath atomically:NO];
[self calcTotal];
[self.tableView reloadData];
}
- (void)addViewControllerDidExit:(AddViewController *)controller
{
[self dismissModalViewControllerAnimated:YES];
[self.tableView reloadData];
}
- (void)addViewControllerDelItem:(AddViewController *)controller
{
NSDictionary *item = [controller values];
if (item != nil)
{
[list removeObjectAtIndex:selItem];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:selIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[list writeToFile:itemFilePath atomically:NO];
}
[self dismissModalViewControllerAnimated:YES];
[self.tableView reloadData];
}
If I exit or delete an item from AddViewController, I have no problems. When I try and add or save, then my application will crash. The debugger crashes at dismissModalViewControllerAnimated. I can't see what's different between the two segment controls.
Any ideas what I may have wrong?
I just discovered that neither the left or right buttons work when I attempt to edit a cell. The editing cell in AddViewController is as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"EditableCell";
EditableCell *cell = (EditableCell *)[table dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[EditableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
NSString *key = [fields objectAtIndex:indexPath.row + indexPath.section * 3];
[cell setLabelText:key];
cell.textField.text = [data valueForKey:key];
if (indexPath.section == 0 && (indexPath.row == 1 || indexPath.row == 3))
cell.textField.keyboardType = UIKeyboardTypeNumberPad;
if (indexPath.section == 1)
cell.textField.keyboardType = UIKeyboardTypeNumberPad;
cell.editing = NO;
cell.delegate = self;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Here is the problem:
[self dismissModalViewControllerAnimated:YES];
[self.tableView reloadData];
When you dismiss the view the value of self become unknown, accessing the tableview will definitely crash.
If you want to refresh the tableView just use [self.tableView reloadData] and don't dismiss the view.