您的位置:首页 > 产品设计 > UI/UE

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

2011-12-06 22:55 489 查看
今天练习的主要目标是:
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。 需要本例子代码的童鞋,请留下电子邮件。



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐