UITableView是很重要的一个View了,这个话题有点大,因此我这里只是简单的分析这个例子给我们提供的信息。关于其他的,我会放在中级或者高级话题里面讲述。
类SimpleTableViewAppDelegate
在这个类中,我们可以学到如何动态生成一个导航模式的viewcontroller,一个table view controller,并且把table view controller作为导航模式的第一个view controller.
RootViewController *rootViewController = [[RootViewControlleralloc] initWithStyle:UITableViewStylePlain];
代码方式生成一个view controller的一个实例,注意这里没有xib文件。
需要说明的是,table view可以加载在一个普通的view controller上,也可以通过继承一个UITableViewController的方式生成,这里使用后一种方式生成。并且初始化为不分组模式(UITableViewStylePlain)。
// Retrieve the array of known time zone names, then sort the array and pass it to the root view controller.
NSArray *timeZones = [NSTimeZoneknownTimeZoneNames];
取得所有系统支持的,时区相关的地址名称,以显示在table view上。
rootViewController.timeZoneNames = [timeZonessortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
这里我们学到了一个排序的方式,通过调用NSArray的函数sortedArrayUsingSelector来排序, 这个函数的传入参数是一个函数,可以是系统提供的函数,也可以是我们自己定义的函数,这里调用定义在NSString中的函数localizedCaseInsensitiveCompare。这个函数实现字符串的大小写不敏感的比较方式。
UINavigationController *aNavigationController = [[UINavigationControlleralloc] initWithRootViewController:rootViewController];
代码的方式生成一个导航模式的view controller。把上面的view controller的实例作为根view controller。
RootViewController
self.navigationController = aNavigationController;
[aNavigationControllerrelease];
[rootViewControllerrelease];
注意上面的内存管理,我们知道,Objective-C的重点有三个,一个是内存管理,二是内存管理,三还是内存管理。
rootViewController所指向的内存在alloc的时候计数为1,在调用UINavigationController *aNavigationController = [[UINavigationController alloc]initWithRootViewController:rootViewController];的时候自动增1,所以这里必须release一次。aNavigationController是同样的方式,最好自己分析一下。
// Configure and display the window.
[windowaddSubview:[navigationControllerview]];
把导航模式的view加载在window上。
类RootViewController
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// There is only one section.
return 1;
}
一个table view分为一个或多个节(section),每一节可以有多行(row)
上面的这个函数就是返回这个table view有多少个节.
这个函数不是必须实现的。如果你不实现,那么这个节数是1。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of time zone names.
return [timeZoneNamescount];
}
上面这个函数返回某一节中有多少行。传入参数就是节所在index。这个函数是必须实现的。- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
staticNSString *MyIdentifier = @"MyIdentifier";
// Try to retrieve from the table view a now-unused cell with the given identifier.
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:MyIdentifier];
// If no cell is available, create a new one using the given identifier.
if (cell ==nil) {
// Use the default cell style.
cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:MyIdentifier] autorelease];
}
上面这几行代码是这个函数一般情况下必须写的,这里牵涉到一个内存管理,cell重用的问题,这个我会放在高级话题中讲,如果你是初级,那么先这么记着就可以了。
// Set up the cell.
NSString *timeZoneName = [timeZoneNamesobjectAtIndex:indexPath.row];
cell.textLabel.text = timeZoneName;
上面的代码更新cell上的文本。需要提到的是,如果你看到某些代码写的是cell.text = @"blabla....";这样的方式,那么这个代码是3.0以前生成的,这样的方式已经被废弃的,请不要使用。
return cell;
}