Switch from UIView to UITableview on press of a button - iphone

I made a button and connected it to an action in interface builder. What do I need to do in the action method to switch from displaying a view to displaying a table view?
Here is some of my code:
// SwitchToTableViewController.h
#import <UIKit/UIKit.h>
#interface SwitchToTableViewController : UIViewController {
}
-(IBAction) switch : (id) sender;
#end
and
//SwitchToTableViewController.m
#import "SwitchToTableViewController.h"
#import "Table.h"
#implementation SwitchToTableViewController
-(IBAction) switch : (id) sender{
// what is i need to write here to switch to tableview
}
#end
and
//table.m
#import <UIKit/UIKit.h>
#interface Table : UITableViewController {
}
#end
and
//table.h
#import "Table.h"
#implementation Table
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
// 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];
}
// Set up the cell...
return cell;
}
#end

You need:
Create an instant of Table.
Pass any data over to this instant, most likely an array so that you can use it to display cells data. You need to create some iVars in your Table class first. Currently you don't have any.
Chose one of many ways to present your Table instant.
Look into Apple's doc. It has quite a bit sample code.
Edit for sample code:
In your SwitchToTableViewController.h file, add this line:
#import "Table.h"
In your SwitchToTableViewController.m, make this changes
-(IBAction) switch : (id) sender{
    // what is i need to write here to switch to tableview
Table *myTable = [[Table alloc]init];
myTable.cellArray = [NSArray arrayWithObjects:#"Item1", #"Item2", #"Item3", nil]; //only an example to be displayed
[self presentViewController:myTable animated:YES completion:nil]; // present your Table - view controller
}
Your Table.h file should look like this:
#import <UIKit/UIKit.h>
#interface Table : UITableViewController {
}
#property (nonatomic, strong) NSArray *cellArray; // adding this line to get data from the parent
#end
In your Table.m, make this changes
#implementation Table
#synthesize cellArray; //add this line right after #implementation Table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return cellArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.text = [cellArray objectAtIndex:indexPath.row];
}
return cell;
}

You have two view controllers here, not two views. It's very important to realise the difference between those two concepts. I've put in bold in the answer below some other key words you need to read up on.
To show another view controller in response to something like a button press, you need to create a new instance of your destination view controller, then get it on the screen somehow.
There are many ways to do this but the simplest way, for a beginner, would be to do all this in storyboards. Add both of your view controllers to the storyboard.
Your initial view controller (with the button) should be embedded in a UINavigationController. Control-drag from the button to your table view controller. This will create a segue. Choose "push" from the list of options.
Hey presto, your table view will now appear on screen when you press the button.

First Create a SingleView Application (UIViewController based) name it as SwitchingView.
Then add a UITableView above the view. Connect using Interface builder. Set delegate and datasource of tableview as the SwitchingView.
Also add a button on the view (before adding table view). Set table view is Hidden using IB(interface builder).
In button Action do
tableview.hidden = NO;
This will show your Table view!!! If you want to show view again, just make
tableview.hidden = YES;
in tableView:didSelectForIndexPathRow method.

Related

UITabBarController with UITableView

i am trying to display a list on my first view so added this in the first.h :
#import <UIKit/UIKit.h>
#interface argospineFirstViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *Journals;
IBOutlet UITableView *myTableView;
}
#property (nonatomic,retain) NSMutableArray *Journals;
#property (nonatomic, retain) UITableView *myTableView;
#end
and then i added this on my first.m :
#implementation argospineFirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Journals=[NSMutableArray arrayWithObjects:#"journal1",#"journal2",nil];
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}
cell.text=[Journals objectAtIndex:indexPath.row];
return cell;
}
I am newbie so i don't really know what kind of connections i have got to make, i am also using a storyboard with a tableview dropped on the first view.
is there something i have to add to the delegate?
Any help?
Thank you for your time
Right click on the tableView in the storyboard. You will see "delegate" and "dataSource" under Outlets. Drag the bubble on the right of those to the view controller icon at the bottom of the view. This will make your viewcontroller the delegate and datasource for the table view if you don't want to do it programmatically.
Do not make property of your table view object.
Also,
in viewDidLoad method write:
myTableView.dataSource = self;
myTableView.delegate = self;
Tell me if it helps!
Use initWithStyle instead of initWithFrame for creating your cell.
In your storyboard, select your table view and open the Connections Inspector. Make sure that the delegate and datasource connections are linked to your argospineFirstViewController object.
in IBOutlet set delegete and datasource of the tableview to filesOwner
you use cell.text for show the data i think its not work in tableview just try this line:-
cell.textlabel.text=[yourArrayname objectatindex:index.row];
no need to connect delegate you already define in protocol.

Table view controller on view controller

I am creating an app in which i want to display a text box and when i enter an word in it ,then it will display the contents from the dictionary which are matching to that word.Now i want to display the list which will generate from my dictionary according to the word entered in text box ,in a table view and that table view should be on the same view controller where i am having the text box.Is it possible to do so. I mean is it possible to create a table view with scrolling option so that user can scroll through the list and then select the word which he wants.
Yes it is possible. Take IBOutlet for UITableView and wire it up. Define its datasource and delegate to your controller. Implement UITableViewDelegate to your controller and override all the methods like cellForRowAtIndex and others.
//FilterDataViewController.h
#import <UIKit/UIKit.h>
#interface FilterDataViewController : UIViewController <UITableViewDelegate>
{
IBOutlet UITableView *tblView;
IBOutlet UITextField *txtFld;
NSMutableArray *arrSrch;
NSMutableArray *srchedData;
}
-(IBAction)srchBtnTapped:(id)sender;
#end
//FilterDataViewController.m
#import "FilterDataViewController.h"
#implementation FilterDataViewController
-(IBAction)srchBtnTapped:(id)sender
{
if(![txtFld.text isEqualToString:#""])
{
[srchedData removeAllObjects];
for (NSString *allStrings in arrSrch)
{
NSComparisonResult result = [allStrings compare:txtFld.text options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [txtFld.text length])];
if (result == NSOrderedSame)
{
[srchedData addObject:allStrings];
}
}
[tblView reloadData];
}
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
arrSrch = [[NSMutableArray alloc] initWithObjects:#"One",#"One Two",#"Two",#"Three",#"Four",#"One Five",#"Six",nil];
srchedData = [[NSMutableArray alloc] init];
}
#pragma mark -
#pragma mark Table view data source
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [srchedData count];
}
// 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];
}
cell.textLabel.text = [srchedData objectAtIndex:indexPath.row];
// Configure the cell.
return cell;
}
#end
This is surely possible. Create a View based Application and put both of your table view and Text Field in the same view and you can do what ever you are planning to do.

UITableView as a subview?

Is this possible? I just want a small table in my current view... here is what I'm trying to do:
.h file
#import <UIKit/UIKit.h>
#interface IngredientsRootView : UIViewController <UITableViewDelegate, UITableViewDataSource> {
UITableView *ingredientsTable;
}
#property (nonatomic, retain) UITableView *ingredientsTable;
#end
.m file I have delegate and data source methods and this code:
ingredientsTable = [[UITableView alloc] initWithFrame:CGRectMake(10, 10, 300, 300) style:UITableViewStylePlain];
[ingredientsTable setDelegate:self];
[ingredientsTable setDataSource:self];
[self.view addSubview:ingredientsTable];
The app doesn't crash, but it doesn't show a table. At the moment I have just set everything definitely as per:
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 10;
}
// 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...
cell.textLabel.text = #"Hello";
return cell;
}
What am I doing wrong? Thanks
Try calling -reloadData on the table view after adding it as a subview.
Also, how is the view set up? Is it created in a XIB or via -loadView?
Did you remembered to add the [window addSubview:[IngredientsRootView view]]; to your app delegate ?
And by the way, instead of inheriting from UIViewController you can just inherit from UITableViewController and you'll get all that functionality for you with no added code.

Add an external TableView inside a small view on the Main Screen

I have a simply MainWindow file created by the Interface Builder in black background. Inside this plain view, I've added from top to bottom a small view (300x100), a textLabel and a couple of buttons.
In the MainApplication.h I've defined:
IBOutlet RoundRectView* smallView;
IBOutlet UILabel* label;
In the MainApplication.h I've #synthesized these variables and with that:
[label setText:#"Test"];
I can see all in my screen and it works perfect.
Now my problem:
I want to add a table with two rows inside my small white view, and what I've done is:
Create a tableController.h who's the delegate and the data source:
#interface IncomeExpenseController :
UITableViewController
{ IBOutlet
UITableView* smallTable; NSArray
*content; }
#property (nonatomic, retain)
UITableView *smallTable; #property
(nonatomic, retain) NSArray *content;
#end
Create the .m file with:
#synthesize smallTable; #synthesize
content;
(void)viewDidLoad {
[super viewDidLoad]; content = [[NSArray alloc]
initWithObjects:#"Item1", #"Item2",
nil]; }
(NSInteger)numberOfSectionsInTableView:(UITableView
*)tableView {
// Return the number of sections.
return 0; }
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 2; }
// 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];
} NSLog(#"inside cell");
// Configure the cell... cell.textLabel.text = [content
objectAtIndex:indexPath.row];
return cell; }
Now in the Interface Builder, I drag and drop a UITableView inside the white small view and drag and drop a Table View Controller along with the File Owner's window and I do these connections:
Class of the Table View Controller to tableController
Outlets:
DataSource and Delegate to my Table Controller
Referencing Outlets:
customTable and View to my Table Controllers
When I execute, I can see the lines of the table view but with no content, I cannot see the NSLog either but if I put a NSLog inside the viewDidLoad I can see it.
I'm really new (2 days) on that and any help would be really appreciated.
Thanks!
Solved.
First of all, the TableView is grouped so I must return almost one section.
Second, from the Interface Builder, I had to link the "view" from the Table to my external controller.
Now it works perfectly.

having two table views in a single xib one of plain style and the other grouped styled

I want to use two tableviews under the segmentedcontrol. one is plain styled and the other is groupstyled. How can i controll the delegate and datasource for the two table views?
--
Regards,
Syed yusuf
HI,
I have to inserted the data into table view by using sqlite.But now i need that data in to two table views in next view..I have arranged segmented bar.Am getting two table views but the values in r not displaying.It is displaying NULL.
Using multiple or two exclusive table in the save UI view controller sharing the same data source.
I face a problem like this, if anybody needs later...
I try to set my ViewController as a datasource for two different table. But it did not work. 2 tables are exclusively shown at loadtime of the view. Either one will be hidden in viewDidLoad depending on a flag. Seems once dataSource is called for one table, it's not called for the 2nd table. All connections are set in IB.
The solution is to set the dataSource in code in viewDidLoad. Then it works.
-(void) viewDidLoad(){
table1.dataSource = self;
table2.dataSource = self;
if(someCondition == YES)
table1.visible = NO;
else
table2.visible = NO;
}
Since you can't change the UITableViewStyle of a UITableView once created (it can only be set at construction time), you have to have two different instances. You can do this in very different ways, but I've done it this way: add a UISegmentedControl to your interface, and set its target to the RootViewController class instance in your application. The RootViewController class could look like this:
#class DataSource;
#interface RootViewController : UITableViewController
{
#private
UITableView *_originalTableView;
UITableView *_secondaryTableView;
DataSource *_dataSource;
BOOL _showingSecondaryTableView;
}
- (IBAction)swap:(id)sender;
#end
And this might be the implementation:
#import "RootViewController.h"
#import "DataSource.h"
#implementation RootViewController
- (void)dealloc
{
[_dataSource release];
[_originalTableView release];
[_secondaryTableView release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
_dataSource = [[DataSource alloc] init];
_secondaryTableView = [[UITableView alloc] initWithFrame:self.tableView.frame
style:UITableViewStyleGrouped];
_secondaryTableView.delegate = _dataSource;
_secondaryTableView.dataSource = _dataSource;
_originalTableView = [self.tableView retain];
_showingSecondaryTableView = NO;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark -
#pragma mark IBAction method
- (IBAction)swap:(id)sender
{
if (_showingSecondaryTableView)
{
self.tableView = _originalTableView;
_showingSecondaryTableView = NO;
}
else
{
self.tableView = _secondaryTableView;
_showingSecondaryTableView = YES;
}
}
#pragma mark -
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (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];
}
cell.textLabel.text = [NSString stringWithFormat:#"RootViewController cell %d", indexPath.row];
return cell;
}
#end
This is the interface of the DataSource class:
#import <UIKit/UIKit.h>
#interface DataSource : NSObject <UITableViewDelegate,
UITableViewDataSource>
{
}
#end
And the implementation:
#import "DataSource.h"
#implementation DataSource
#pragma mark -
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"DataSourceCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [NSString stringWithFormat:#"DataSource cell %d", indexPath.row];
return cell;
}
#end
You can change the datasource and delegate of the UITableView instances to anything you want, at any time during runtime, which might help you encapsulate different data sources with separate controllers.
Hope this helps!