Home > Tutorials > Non-Sectioned Table View

Non-Sectioned Table View

June 11th, 2009


To create a non-sectioned table view you must first insert a table view into your view. Once you have done this, set its delegate to a class you have defined. We will now try to create a user interface like the one below:

You must first add the delegate protocol to the top of your class:

@interface LogView : UIView 

Now that this is done, we must implement our DataSource methods to display information in the table. The first method is numerOfSectionsInTableView, which tells the UITableView how many sections there are in this table. Now since we are dealing with a standard table, there is only 1 section, that being the main table.

-(NSInteger) numberOfSectionsInTableView:(UITableView*)tableView
{
	return 1;
}

The next is numberOfRowsInSection, this is where we get to define how many rows are table is going to take up, now the table we are trying to recreate above has 4 rows, so we will simply return 4 to indicate this:

-(NSInteger) tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
	return 4;
}

The last method we have to implement is cellForRowAtIndexPath, this will return the UITableViewCell at the a specific row. We get the row by querying indexPath.row, and we then return the text based on this.

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
	switch(indexPath.row)
	{
		case 0: return @"iPhone";
		case 1: return @"iPod";
		case 2: return @"MacBook";
		case 3: return @"MacBook Pro";
	}
	return @"Unknown Table";
}

And that is all you need to do to make a very simple non-sectioned table view.

Author: admin Categories: Tutorials Tags:
  1. No comments yet.
  1. No trackbacks yet.