我的iphone开发学习笔记(七): 使用UITableViewController

今天练习的主要目标是:

a.  使用UITableViewController显示列表数据

b.  响应列表的选择操作;

1. 创建window_base_application

    名称为FlowerColorTable

     

2. 创建一个文件UITableViewController, 文件名

     FlowerColorTableViewController

      

3. 在 FlowerColorTableAppDelegate.h

    声明FlowerColorTableViewController

    

@interface FlowerColorTableAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
	IBOutlet FlowerColorTableViewController	*flowerColorTableViewController;
}

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

4. 打开MainWindow.xib

    添加UITableViewController

    修改其Class identity,为

    FlowerColorTableViewController

    

5. FlowerColorTableAppDelegate.h

    [window addSubView: flowerColorTableView.view];

    同时注意释放该flowerColorTableViewController

    代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.
    
    [window makeKeyAndVisible];
    [window addSubview:flowerColorTableViewController.view];
    return YES;
}


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

6. 添加具体的数据table数据源

    打开FlowerColorTableViewController.h

    声明redFlowers, blueFlowers数组

- (void)viewDidLoad {
    [super viewDidLoad];
	redFlowers = [[NSArray alloc]initWithObjects:@"Gerbera",@"Peony",@"Rose",@"Poppy",nil];
	blueFlowers = [[NSArray alloc]initWithObjects:@"Hyacinth",@"Hydranges",@"Sea Holly",nil];
}

7. numberOfSectionInTableView

    重写几个函数,分别表示secion个数、各个Section的元素个数、以及各个section的title

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return sectionCount;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
	switch (section) {
		case redSection:
			return [redFlowers count];
		case blueSection:
			return [blueFlowers count];
		default:
			return 0;
	}
}

8. 填充分区标题

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
	switch (section) {
		case redSection:
			return @"redFlower";
		case blueSection:
			return @"blueFlower";
		default:
			return @"Unknow";
	}
}

9。填充表格单元格

- (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];
    }
    
	switch (indexPath.section) {
		case redSection:
			[[cell textLabel]setText:[redFlowers objectAtIndex:indexPath.row]];
			break;
		case blueSection:
			[[cell textLabel]setText:[blueFlowers objectAtIndex:indexPath.row]];
			break;

		default:
			break;
	}
    return cell;
}

其中的主要技术点:如果cell==nil, 则获得一个可以服用的tableCell

         其次,通过indexPath.row作为索引,从数组中提取value

         最后,将value赋给cell textLabel


10. 响应选中操作

      这个方法也比较关键。      

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	NSString *showMessage;
	switch (indexPath.section) {
		case redSection:
			showMessage = [[NSString alloc] initWithFormat:@"%@",[redFlowers objectAtIndex:indexPath.row]];
			break;
		case blueSection:
			showMessage = [[NSString alloc]initWithFormat:@"%@",[blueFlowers objectAtIndex:indexPath.row]];
			break;
		default:
			showMessage = @"unknown";
	}
	
	UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"selected notice" message:showMessage delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
	[alert show];
	[alert release];
}

这个例子完成,希望对大家有所帮助。

声明:

1。上述例子代码来源于书籍<<Iphone开发入门经典>>,经过本人的实际练习通过。

2 需要本例子代码的童鞋,请留下电子邮件。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值