UITableView是一种“目录视图或叫表视图”(英文名字table view),这种表视图以列表的形式显示或编辑信息,它由一列、多行组成。用户可以通过垂直滚动的方式导航到一个表视图的任意行上,并可以自定义每一行数据的显示方式。
在创建表视图的时候,可以选择两种风格的表视图:UITableViewStylePlain或者UITableViewStyleGrouped,前者是按索引进行排序的,而后者是按组进行分类显示的。
基本上每一个UITableView都有相应的UITableViewController、UITableViewDelegate、UITableViewDataSource类。UITableViewController类作为UITableView的视图控制类(MVC里Controller的角色)负责管理UITableView,它和大多数UIViewController类一样,控制着UITableView的生命周期函数。UITableViewDelegate作为委托模式里的被委托对象的接口,和大多数iPhone委托接口一样,它提供了UITableView子类无法在行为上保持一致的部分,在这里读者可以自定义表视图的显示风格,甚至可以自定义表视图的每一个元素,它的重要接口定义如下:
@protocol UITableViewDelegate<NSObject, UIScrollViewDelegate> @optional
// Display customization - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
// Variable height support - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
// Section header & footer information. Views are preferred over title should you decide to provide both - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
// Accessories (disclosures). - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
// Selection // Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection. - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
// Called after the user changes the selection. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
// Editing - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
// Indentation - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath; // return 'depth' of row for hierarchies
@end
UITableViewDataSource提供了表视图的数据源,下表列出了常见的表视图数据源方法:
表视图数据源接口提供了表视图数据源操作的常用方法,其中tableView:numberOfRowsInSection和tableView:cellForRowAtIndexPath:是每一个表试图的数据源必须实现的两个方法,前者告诉表视图内有多少行单元格,而后者告诉表视图每一个单元格的内容是什么。程序通过实现这两个方法,可以提供一个表视图所需要的基本信息并供表视图调用。
笔者在下面的例子里会编写一个带导航面板的表视图,这种复杂类型的控件在iPhone中随处可见,如iPod程序、备忘录程序、闹钟程序等。
ü 首先,新建一个“Window-based”项目并命名为“TableProjectOne”。 ü 新建一个UIViewController的子类,并命名为“MyViewController”。 ü 创建一个空的xib并命名为“MyViewController”。 ü 在Interface Build中打开MyViewController.xib。 ü 从Library里拖拉一个view到MyViewController的窗口中,然后再添加一个table view到新添加的view下,确保table view完全填充view。 ü 修改File’s Owner为MyViewController,连接MyViewController的view到上面新添加的view上。 ü 到这里MyViewController创建完毕,保存并退出Interface Build。 ü 打开MainWindow.xib并拖拉一个Navigation Controller到窗口中。 ü 拖拉一个View Controller到Navigation Controller下,改变它的类名和Nib名为MyViewController。 ü 拖拉一个Bar Button Item到Navigation Controller上,并改title为“view2”。
修改TableProjectOneAppDelegate.h文件如下:
#import <UIKit/UIKit.h> #import "MyViewController.h"
@interface TableProjectOneAppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window; IBOutlet UINavigationController *viewController; }
@property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UINavigationController *viewController;
@end
实现文件TableProjectOneAppDelegate.m如下:
#import "TableProjectOneAppDelegate.h"
@implementation TableProjectOneAppDelegate
@synthesize window; @synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application { [window addSubview:self.viewController.view]; // Override point for customization after application launch [window makeKeyAndVisible]; }
- (void)dealloc { [window release]; [viewController release]; [super dealloc]; } @end
最终实现的效果如下:
|
|