我的iphone开发学习笔记(七): 使用UITableViewController
2011-12-06 22:55
489 查看
今天练习的主要目标是:
a. 使用UITableViewController显示列表数据
b. 响应列表的选择操作;
1. 创建window_base_application
名称为FlowerColorTable
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/22/4a269383596a7f098f326abcbb05d098.gif)
2. 创建一个文件UITableViewController, 文件名
FlowerColorTableViewController
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/22/a484d0bd3bf51521361b5b75b34f49ca.gif)
3. 在 FlowerColorTableAppDelegate.h
声明FlowerColorTableViewController
4. 打开MainWindow.xib
添加UITableViewController
修改其Class identity,为
FlowerColorTableViewController
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/22/c5b96dc46453db499c0d5c12c3b66d88.gif)
5. FlowerColorTableAppDelegate.h
[window addSubView: flowerColorTableView.view];
同时注意释放该flowerColorTableViewController
代码:
6. 添加具体的数据table数据源
打开FlowerColorTableViewController.h
声明redFlowers, blueFlowers数组
7. numberOfSectionInTableView
重写几个函数,分别表示secion个数、各个Section的元素个数、以及各个section的title
8. 填充分区标题
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。 需要本例子代码的童鞋,请留下电子邮件。
a. 使用UITableViewController显示列表数据
b. 响应列表的选择操作;
1. 创建window_base_application
名称为FlowerColorTable
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/22/4a269383596a7f098f326abcbb05d098.gif)
2. 创建一个文件UITableViewController, 文件名
FlowerColorTableViewController
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/22/a484d0bd3bf51521361b5b75b34f49ca.gif)
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
![](https://oscdn.geek-share.com/Uploads/Images/Content/201912/22/c5b96dc46453db499c0d5c12c3b66d88.gif)
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。 需要本例子代码的童鞋,请留下电子邮件。
相关文章推荐
- 我的iphone开发学习笔记(二)使用UITextView, UITextField 和UIButton控件
- iPhone开发学习笔记005——使用XIB自定义一个UIView,然后将这个view添加到controller的view
- iPhone开发学习笔记005——使用XIB自定义一个UIView,然后将这个view添加到controller的view
- ios学习笔记(一)Windows7上使用VMWare搭建iPhone开发环境(转)
- iPhone开发学习笔记_剪切板的使用
- ios学习笔记(一)Windows7上使用VMWare搭建iPhone开发环境
- iPhone开发学习笔记005——使用XIB自定义一个UIView,然后将这个view添加到controller的view
- ios学习笔记(一)Windows7上使用VMWare搭建iPhone开发环境
- 我的iphone开发学习笔记(三)使用UISlide, UIImageView及动画
- IOS 学习笔记17 iPhone开发之使用NSUserDefaults存储数据
- iPhone开发学习笔记[3/50]-使用工具栏创建简单的多视图应用程序
- iPhone开发学习笔记[4/50]-表视图的使用
- 我的iphone开发学习笔记(九): 使用NSUserDefaults读写用户默认设置
- ios学习笔记(一)Windows7上使用VMWare搭建iPhone开发环境
- 我的iphone开发学习笔记(六): 使用选项卡栏管理多视图界面
- 我的iphone开发学习笔记(四)使用UIWebView,UISegmentedControl
- ios学习笔记(一)Windows7上使用VMWare搭建iPhone开发环境
- 学习笔记:Linux驱动开发git基本使用流程
- iPhone开发学习笔记002——Xib设计UITableViewCell然后动态加载
- iphone开发学习笔记-2