How can i add objects into UITableView? - iphone

How can i add objects into UITableView?

You have to first take IBOutlet Object of UITableView in interface file and then bind it using interface builder. Set delegate and data source of UITableView in the interface builder property.
And do the following coding in your project.
In .h file:
#interface RootViewController : UITableViewController<UITableViewDelegate,UITableViewDataSource> {
IBOutlet UITableView *tblView;
}
In .m file:
// 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 n;// n for number of row.
}
// 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.
return cell;
}

Implement the UITableViewDataSource delegate methods.
Also check out the Table View Programming Guide.

Tableviews are populated by datasources, datasources are often an array of objects.
I suggest you look through some tutorials on tableviews and also delegate methods.
http://icodeblog.com/2008/08/08/iphone-programming-tutorial-populating-uitableview-with-an-nsarray/
icodeblog is a fantastic website for beginners.

Related

Tableview inside Flipview using Utility template

I'm new to iOS dev, so I do apologise for asking potentially dumb question.
What I want to do is something very similar to the default Weather app; where the app has a info button, it flips to another view which has a table along with a Done button to go back to the app.
I'm using the 'Utility Application' template which does most of this for me :)
However, I am now struggling to add a tableview into the flipview. Am I on the right path? I'm using storyboard at the moment - beginning to realise this is most likely a limitation on the GUI (after all a GUI can only go so far). If so, is this possible programatically and how would I go about applying it on a default 'Utility Application' template.
I'm using Xcode 4.2.
Any help would be appreciated. Thanks in advance :)
First of all, you'll want to drag and drop a UITableView onto your flipsideViewController in interface builder. Make sure that you like its delegate and data source to the view controller.
Then change flipsideViewController.h to create an instance variable for the array that will store the text for the cells labels and for the controller to conform to the tables delegate and data source methods.
#interface FlipsideViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
NSArray *myArrayOfItems;
}
In flipsideViewController.m alloc/init and populate your array in viewDidLoad
myArrayOfItems = [[NSArray alloc] initWithObjects:#"firstItem",#"secondItem",#"thirdItem",#"fourthItem", nil];
And finally, copy and paste the following and you should have a working table!
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [myArrayOfItems count];
}
- (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];
}
cell.textLabel.text = [myArrayOfItems objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Selected cell index:%i",indexPath.row);
}

Switch from UIView to UITableview on press of a button

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.

Linking UITableView with a NSMutableArray

I've been trying to fill a UITableView with data from a NSMutableArray. I have a View-Based application project and so far I've tried every tutorial I found but so far, it just doesn't work. I linked the dataSource and delegate to the File's Owner, I added the procotols UITableViewDelegate and UITableViewDataSource to my UIViewController, I create an array containing 3 strings just to try and debug, everytime I load the app, the table stays empty.
I'm not sure what could be my error, or where I should look to find the problem, if you guys got any idea that would be nice.
Thank you
-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection: (NSInteger)section
{
return [yourArray count];
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [yourArray objectAtIndex:row];
return cell;
}
That should populate the table from an array. I've used it several times in my app.
I guess you did forget to call -reloadData on the table view.
Make sure the dataSource and delegate are properly connected to the view controller, also make sure your UITableView is property connected with the outlet if you are using storyboards or a xib file.
If nothing works I recommend you to use XLData that makes UITableView data loading much more simple to implement.

How to show text in a UITableView

I have a table and I want to show there some text; but I can't, I don't know why. My app is a view-based app, and I don't want to change it for a Table View Controller. Here's the code:
.h
{
NSArray *array;
IBOutlet UITableView *table;
}
.m
- (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 [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.text = [array objectAtIndex:indexPath.row];
// Configure the cell
return cell;
}
Please help me because I'm trying to solve this and I don't know what's wrong, why the Table doesn't show the array!
Thank you!
You almost certainly haven't connected your datasource and delegates properly in IB.
Your UIViewController needs to implement UITableViewDelegate and UITableViewDatasource, and you need to then connect it to your UITableView (table) as such in Interface Builder.
For details, read through Apple's documentation here.
On your .H file are you using this interfaces <UITableViewDelegate, UITableViewDataSource>?
The answer is that you've never ever created single cell. The call you make dequeueReusableCellWithIndentifier only recycles cells you've already created. Add this code after the dequeueResusable... line:
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

Using UITableViewCell in a UITableView

I'm a little confused with something. I'm trying to create a custom cell and I want to use the interface builder way.
The normal way I create a table is to have the table as:
.h
#interface AssessList : UIViewController {
IBOutlet UITableView *tblAssessList;
}
#property(nonatomic, retain) UITableView *tblAssessList;
#end
.m
- (NSInteger)
numberOfSectionsInTableView:(UITableView *)tableView {
return groupArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return totalArray.count;
}
- (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] autorelease];
}
cell.textLabel.text = #"I am the text....";
return cell;
}
Now i've created a new class for the cell and I think I understand how to put that in. But can I leave the .h as
#interface AssessList : UIViewController
or does the class/nib with the full table on it have to be a UITableViewController?
Tom
does the class/nib with the full table on it have to be a
UITableViewController?
No. A UITableViewController is just a convenience UIViewController subclass which has a UITableView and is already setup as its delegate/datasource (it is declared as conforming to the UITableViewDelegate and UITableViewDatasource protocols), it also has pre-filled method implementations for these protocols in the template implementation file which Xcode generates for you. You can just as well do all of this yourself, I often do.
You should however make an IBOutlet for your UITableViewCell so that you can load it from the nib file (see the Loading Custom Table-View Cells From Nib Files in the Table View Programming Guide).
If you want to do in the Interface Builder Way, then create an xib (view xib). Drag and drop a UITableViewCell object from the obj palette. Customize it as you wish. In the tableView:cellForRowAtIndexPath: method, do this:
UITableViewCell * aCell = [tableview dequeueReusableCellWithIdentifier:#"SomeIdentifier"];
if (aCell == nil)
{
NSArray *arr = [[NSBundle mainBundle] loadNibNamed:#"CustomCellNibName" owner:self options:nil];
for (NSObject *anObj in arr) {
if([anObj isKindOfClass:[UITableViewCell class]]) {
aCell = (UITableViewCell *)anObj;
}
}
}
The identifier for the tableviewcell can be set in the IB.
I guess it should be sub class of UItableViewCell
i.e.
#interface AssessList : UITableViewCell
When you want a custom tableview cell you will also need a subclass of UITableViewCell..
A tutorial can be found on this blog
Keep in mind that quite a few things can be done without creating a custom cell, this includes adding the switch to make your tableview look like the one from settings.app, to the way the iPod displays songs.
In the assessList Class you are using the custom cell created in otherviewController (UITableViewCell subclass) so there is no need to change this line
#interface AssessList : UIViewController
Note:- the otherviewController should be a subclass of UITableViewCell