no

How to Implement a Uitableview in Xcode

In this demo I will extend the class UITableViewController, so I already have access to UITableViewDelegate and UITableViewDataSource classe...

In this demo I will extend the class UITableViewController, so I already have access to UITableViewDelegate and UITableViewDataSource classes.

Steps:
1.) Create a new window based project. Named it MyTable.

2.) I will explain the rest in codes:
MyTableAppDelegate.h
#import <UIKit/UIKit.h>
#import "MyTableViewController.h"

@interface MyTableAppDelegate : NSObject  {
 UIWindow *window;
 MyTableViewController *myTableViewController; //custom class that extends UITableViewController
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@end
MyTableAppDelegate.m
#import "MyTableAppDelegate.h"

@implementation MyTableAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
 //call the custom class initializer
 myTableViewController = [[MyTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
 myTableViewController.view.frame = [UIScreen mainScreen].applicationFrame;
 
 //add to the main window
 [window addSubview:myTableViewController.view];
    // Override point for customization after application launch
    [window makeKeyAndVisible];
}


- (void)dealloc {
    [window release];
    [super dealloc];
}

@end
#import <Foundation/Foundation.h>

@interface MyTableViewController : UITableViewController {
 NSMutableArray *names; //table view data
}

@end
#import "MyTableViewController.h"

@implementation MyTableViewController

- (id)initWithStyle:(UITableViewStyle)style {
 self = [super initWithStyle:style];
 if(self) {
  names =  [[NSMutableArray alloc] initWithObjects:@"Ichi", @"Ni", @"San", nil];
 }
 return self;
}

- (void)viewWillAppear:(BOOL)animated {
 [super viewWillAppear:animated];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DefaultCell"];
 if(cell == nil) {
  cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"DefaultCell"] autorelease];
 }
 cell.text = [names objectAtIndex:indexPath.row];
 return cell;
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
 return [names count];
}

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

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
 return [NSString stringWithFormat:@"Section %i", section];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 NSString *name = [names objectAtIndex:indexPath.row];
 NSString *title = [NSString stringWithFormat:@"%@ selected!", name];
 UIAlertView *view = [[UIAlertView alloc] initWithTitle:title message:@"Pressed" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
 [view show];
 [view release];
 
 [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {
 return UITableViewCellAccessoryDisclosureIndicator;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
 return [NSArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", nil];
}

- (void) dealloc
{
 [names release];
 [super dealloc];
}

@end

Related

xcode 4794873144115151716

Post a Comment Default Comments

item