I'm little confused of how to implement simple uitableview based menu.Something like in Kayak app http://www.sendspace.com/file/igv31p
My storyboard look like this: navigation controller --->master view controller --> detail view controller. In master view controller i've got a table view. I populated it dynamically with 4 cells which are Products, Stores, Packages and Finances. All I want to do is to display child uiviewcontroller that will be a child uiviewcontroller. Something like this:
Products --> UiViewController with specific functionality
Stores --> Other UIViewControlelr with specific functionality
... and so on.
I create didselectrowatindex method
and try to do something with pushViewController like in code below method but it doesn't work.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
StoresViewController *storeView = [[StoresViewController alloc]init];
switch (indexPath.section) {
case 0:
switch (indexPath.row) {
case 0:
{
NSLog(#"Products");
}
break;
case 1:{
[self.navigationController pushViewController:storeView animated:YES];
NSLog(#"Stores");
}
break;
case 2:{
NSLog(#"Packages");
}
break;
case 3:{
NSLog(#"Finances");
}
break;
}
break;
default:
break;
}
I would appreciate if someone can help me to understand it. If this question was asked before please show me where:) Thanks in advance
Something like in Kayak app
http://www.sendspace.com/file/igv31p
Just replace this code..
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
switch (indexPath.section) {
case 0:
switch (indexPath.row) {
case 0:
{
NSLog(#"Products");
}
break;
case 1:
{
UINavigationController *navigationController = [self.storyboard instantiateViewControllerWithIdentifier:#"StoresViewController"];
[self presentViewController:navigationController animated:YES completion:nil];
NSLog(#"Stores");
}
break;
case 2:
{
NSLog(#"Packages");
}
break;
case 3:
{
NSLog(#"Finances");
break;
}
break;
default:
break;
}
}
StoresViewController *storeView = [[StoresViewController alloc]initWithNibName:#"StoresViewController" bundle:nil];
simply passed the nib when you create the objcet.
Related
On my view, I used to have few buttons and each button had an action associated with it.
UIButton *testButton = [[UIButton alloc] initWithFrame:CGRectMake(120,300,90,90)];
[testButton setBackgroundImage:[UIImage imageNamed:#"test.jpg"] forState:UIControlStateNormal];
[testButton addTarget:self.view action:#selector(gotoProd:) forControlEvents:UIControlEventTouchUpInside];
[testButton addt
[scrollView testButton];
But now I am trying to replace all those buttons with the tableview with rows. I was able to populate the rows and I know one needs to use didSelectRowAtIndexPath for handling on select event of the cell. But how can I implement action:#selector(gotoProd:) in tableviews ?? Any help will be greatly appreciated.
The most straight-forward way would look like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row) {
case 0:
[self doRow0Action];
break;
case 1:
[self doRow1Action];
break;
// etc...
default:
break;
}
}
If you wanted to instead, you could initialize an array with SEL types:
[actionArray addObject:#selector(doRowNAction)];
then access it like this:
[self performSelector:[actionArray objectAtIndex:indexPath.row] withObject:nil];
Call
[self gotoProd:indexPath.row];
from didSelectRowAtIndexPath
and
- (void)gotoProd:(int)rowSelected {
//Check row index here and do it accordingly
}
I'm working with table view and sections...In some case I need do remove one section, in another set it back. I can do that in 2 ways:
1)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (somecase) {
return 2;
} else {
return 3;
}
}
and than
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *tableViewCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
[tableViewCell autorelease];
tableViewCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if (sectionNumber == 3 {
switch (indexPath.section) {
case 0:
break;
case 1:
break;
case 2:
break;
}
} else if (sectionNumber == 2) {
switch (indexPath.section) {
case 0:
break;
case 1:
break;
}
return tableViewCell;
}
}
And a second way is to implement
[self.tableView beginUpdates];
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:NO];
[self.tableView endUpdates];
But it's not working...I put this methods to ViewDidLoad, is that ok ? Or I should put in somewhere else ? Or what is the problem ?
And what is the best practice for removing section ? first one or second ? thanks....
set condition that will satisfy "somecase " then reload tableview
[tableview reloadData];
Hi guys i have different section in my tableview ( more than 6) and each one has different rows. But only one of them leads to next view controllers
here is the code but when i click it its not working
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
switch (section)
{
case 5:
{
switch (row)
{
case 0:
{
Language_view_controller * language_controller = [Language_view_controller alloc];
[self.navigationController pushViewController:language_controller animated:YES];
[language_controller release];
}
break;
default:
break;
}
}
break;
default:
break;
}
}
You are just allocating the memory for your view controller, you also need to initialize it by doing something like this:
Language_view_controller * language_controller = [[Language_view_controller alloc] initWithNibName:#"Language_view_controller" bundle:nil];
[self.navigationController pushViewController:language_controller animated:YES];
[language_controller release];
My application have UI similar to Phone.app->Recents: sectioned UITableView and a UISegmentedControl in the navigation bar. What I want to do is display full set of data if first section is selected and display filtered set of data if second section is selected.
When user selects second item in UISegmentedControl I delete specific rows from the table view. Here is the code:
[tableView beginUpdates];
NSMutableArray *indexPaths = [NSMutableArray array];
/// ... fill up indexPaths with row indexes
[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
The code above works fine except for one serious issue: performance. Deleting 1500 out of 2200 rows takes about 20 seconds. That is obviously unacceptable. What is the best approach to filtering table view rows with animation?
For large changes to your data source, it is recommended that you use
[tableView reloadData]
instead of
[tableView beginUpdates];
// changes here ....
[tableView endUpdates];
EDIT: I haven't tried this approach myself, but consider altering only those rows that are contained in the collection of visible cells, perhaps with a buffer above and below. You can get the indexPaths of the visible cells by calling
[tableView indexPathsForVisibleRows];
How about using a two arrays? One of them is the full data set and the other one is the filtered dataset.
This way you can have two different tableviews, and depending on what the selected segment is, you could do a fade animation between the two tableviews. For example, lets say you select a segment:
-(void)switchTableViews
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDidStopSelector:#selector(hideTableView)];
switch (segmentedControl.selectedSegmentIndex)
{
case 0:
{
tableView1.alpha = 1.0;
tableView2.alpha = 0.0;
}
break;
case 1:
{
tableView1.alpha = 0.0;
tableView2.alpha = 1.0;
}
break;
default:
break;
}
[UIView commitAnimations];
}
- (void)hideTableView
{
switch (segmentedControl.selectedSegmentIndex)
{
case 0:
{
tableView1.hidden = NO;
tableView2.hidden = YES;
}
break;
case 1:
{
tableView1.hidden = YES;
tableView2.hidden = NO;
}
break;
default:
break;
}
}
Of course, this would mean setting up different code sets for the datasource methods but it's not as difficult as you think. Just use a simple if-else to check for which tableview is being set.
I definitely agree with #nduplessis that you should reload the dataSource rather than manipulating the view. I proposed a solution to a similar question here that would indeed cause your rows to slide up.
The basic idea is to call reloadSections:withRowAnimation: and in your UITableViewDataSource methods switch on the segmented control's selectedSegmentIndex.
Assuming your data is flat (only one section) it would look something like this:
- (IBAction)segmentSwitch:(id)sender
{
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (self.segmentedControl.selectedSegmentIndex)
{
default:
case 0:
return [self.allRows count];
case 1:
return [self.onlySomeRows count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
id data;
switch (self.segmentedControl.selectedSegmentIndex)
{
default:
case 0:
data = [self.allRows objectAtIndex:[indexPath row]];
break;
case 1:
data = [self.onlySomeRows objectAtIndex:[indexPath row]];
break;
}
//TODO: use data to populate and return a UITableViewCell...
}
Okay, so this is weird
I have this code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row) {
case 1:
NSLog(#"Platform Cell Selected");
AddGamePlatformSelectionViewController *platformVC =
[[AddGamePlatformSelectionViewController alloc]
initWithNibName:#"AddGamePlatformSelectionViewController" bundle:nil];
platformVC.context = context;
platformVC.game = newGame;
[self.navigationController pushViewController:platformVC animated:YES];
[platformVC release];
break;
default:
break;
}
}
Which works fine.
When I remove the NSLog Statement, like so:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row) {
case 1:
//NSLog(#"Platform Cell Selected");
AddGamePlatformSelectionViewController *platformVC =
[[AddGamePlatformSelectionViewController alloc]
initWithNibName:#"AddGamePlatformSelectionViewController" bundle:nil];
platformVC.context = context;
platformVC.game = newGame;
[self.navigationController pushViewController:platformVC animated:YES];
[platformVC release];
break;
default:
break;
}
}
I get the following compiler errors
/Users/DVG/Development/iPhone/Backlog/Classes/AddGameTableViewController.m:102:0 /Users/DVG/Development/iPhone/Backlog/Classes/AddGameTableViewController.m:102: error: expected expression before 'AddGamePlatformSelectionViewController'
/Users/DVG/Development/iPhone/Backlog/Classes/AddGameTableViewController.m:103:0 /Users/DVG/Development/iPhone/Backlog/Classes/AddGameTableViewController.m:103: error: 'platformVC' undeclared (first use in this function)
If I just edit out the two // for commenting out that line, everything works swimingly.
You can't declare an object (e.g. AddGamePlatformSelectionViewController *platformVC) as the first line in case...
You can solve it by adding some code before than (e.g. NSLog) or by enclosing the code inside the case between { ... } like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row) {
case 1:
{
AddGamePlatformSelectionViewController *platformVC = [[AddGamePlatformSelectionViewController alloc]
initWithNibName:#"AddGamePlatformSelectionViewController" bundle:nil];
// the rest of the code...
break;
}
}
}
Do you get the same error if you delete the NSLog statement instead of commenting it out? Maybe the compiler just doesn't like that you're starting a case block with a comment. (Ridiculous, I know, but worth a shot?)