iOS5编程--官方例子代码的研究--3. TableViewSuite--2_SimpleSectionedTableView
关于table view,分为两种方式,一种是UITableViewStylePlain,另一种是UITableViewStyleGrouped。两种模式都把行row分为若干个节section。每个section都有一个头部和足部,并且整个table view也有一个头部和足部,不同的是,我们只能设置section的头部和足部的字符串,而整个table view的头部和足部是两个view。这些头部和足部都是可选的,下面是图片说明:
上面是演示如何通过xib方式设置两种方式,在右面的红框中是设置grouped 或者 plain。左面是table view设置成grouped的方式的示意图,这里上面的红框是section的头部的字符串,可以看出,这个字符串是靠左,下面的红框是足部,是局中。
这个是grouped的方式下没有设置任何头部和足部,生成的效果。
这个是设置成plain的方式,但是包含section的头部和足部。
这个是plain方式,不包含头部和足部
plain方式,只包含头部,注意这是本文需要分析的例子工程运行的效果图,注意在table view上下滚动的时候的section的头部的变化。这里不再描述,你需要自己运行一下看看。
这里是一个grouped方式,上面的红框是整个table view的头部,中间的是section的头部,最下的两个button是在整个table view的足部。
在我们当前的例子中,我们可以学到两个部分的内容,一个就是关于时区的使用,另一个是table view,为了focus在我们这个文章的重点,我这里只分析table view部分
这个例子中关于table view的部分和上一个例子基本相同,不同的是,上一个例子中tableview只有一个section,这里是有多个section。而且增加了一个函数。
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// The header for the section is the region name -- get this from the region at the section index.
Region *region = [regionsobjectAtIndex:section];
return [regionname];
}
这个函数就是返回每一节的头部的文本。
如果你有兴趣,可以增加一个函数如下,运行一下看看效果
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
Region *region = [regions objectAtIndex:section];
return [regionname];
}
最后说明的一个是在
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
的参数indexPath,是一个NSIndexPath类型。你可以通过indexPath得到section和row,分别为indexPath.row和indexPath.section.