• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

[OBJ-C] Creating and populating a UITableView

Joined
Sep 2, 2006
Messages
1,965
Reaction score
33
So i had fun writing the last one, and i have a bunch of spare time on my hands, so here's one about how to make and populate a UITableView

I named my file TableViewPush because it also "pushes" to other views.

I DO MY COMMENTING IN THE CODE ITSELF USING //COMMENTS

RootViewController.h

Declaring arrays to setup with table.
Code:
interface RootViewController : UIViewController < UITableViewDelegate, UITableViewDataSource> {


	NSMutableArray *array;

RootViewController.m

Here's where we set up the arrays / header of the file
I didn't bother uncommenting the viewDidLoad in the premade .m, so you can just copy paste this below the #pragma's
Code:
- (void)viewDidLoad {
    [super viewDidLoad];

	array = [[NSMutableArray alloc] init];
	[array addObject:@"RaGEZONE"];  //Name of an array
	[array addObject:@"Omer"]; // Name of an array
	
	self.title =@"Raid Guide"; // Header title, one that will show up above your table
}


Configuring the way it looks

After dragging in a UITableView into your Interface builder ( i replaced view with uitableview in the xib) You should now have a section that looks like this :

Code:
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1; // 1 = number of sections
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [array count]; //your going to want to return the array number because you already set up their names
}

Here's where we customize the appearence
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.

	
	cell.textLabel.text = [array objectAtIndex:indexPath.row];
    UIImage *cellImage = [UIImage imageNamed:@"apple.jpg"]; // NOTE : This adds and icon r next to you arrays, replace apple.jpg with w/e image you want, you need to comment out if you have no image.
	cell.imageView.image = cellImage;

	
	return cell;
	
}

Under #pragma mark table view delagate
This is not mandatory, this will push to a new view on click. the first line after the -void line is mandatory for app store release, as it adds in the "untouch" affect on pressing/letting go of rows.
I added in my personal code ( yes this is wow releated, doing something with a wow playing friend lol.)

ill give an explanation of the first if statement, till the first }, since the second part is the same


Code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	
	if ([[array objectAtIndex:indexPath.row] isEqual:@"Icecrown Citadel"]) //If user clicks on the cell named insidde the isEqual@: ( in my case Icecrown Citadel"
	{
		ICC *icc = [[ICC alloc] initWithNibName:@"Icecrown Citadel" bundle:nil]; //will push to the file with the name in caps. which means, if you want a dog file, you would put dog in caps where dog is in cpas and dog in non caps where it isn't

		[self.navigationController pushViewController:icc animated:YES];
		[icc release];
	}
	
	else if ([[array objectAtIndex:indexPath.row] isEqual:@"Trial of the Crusader"])

		{
			TOC *toc = [[TOC alloc] initWithNibName:@"Trial of the Crusader" bundle:nil];
			[self.navigationController pushViewController:toc animated:YES];
			[toc release]; 
		}
	}

You should get something like this :

Main menu

Omer - [OBJ-C] Creating and populating a UITableView - RaGEZONE Forums



Clicking on Icecrown Citadel

Omer - [OBJ-C] Creating and populating a UITableView - RaGEZONE Forums


Request any tutorials you want, i enjoy making these. ill also possibly make any iphone apps you want, so just comment/throw me a pm
 
Last edited:
Back
Top