1、控件属性
1)Style:指定表格的风格,支持两个属性值
- Plain:指定该表格使用最普通的风格
- Grouped:指定该表格使用分组风格
2)Separator:指定表格行之间分隔条的样式,该区域可配置两个属性
- 分隔条样式:该分区的第一个列表框用于设置分隔条样式,该列表框可选择Single Line(单线)和Single Line Etched(被蚀刻单线)
- 分隔条颜色
3)Selection:控制该表格的选择风格,该属性对于的列表框支持如下属性值
- No Selection:不允许选中
- Single Selection:只允许单选
- Multiple Selection:允许多选
4)Editing:控制当表格处于编辑状态是否允许选择,该属性对应的列表框可支持如下属性值
- No Selection During Editing:编辑状态时不允许选中
- Single Selection During Editing:编辑状态时只允许单选
- Multiple Selection During Editing:编辑状态允许多选
2、获取UITableView对象后的属性与方法
1)style:只读属性,用于返回表格的样式,该属性可能返回UITableViewStylePlain(普通)和UITableViewStyleGrouped(分组)样式
2)rowHeight:属性,返回或设置表格的行高,建议通过实现表格对应的委托对象的tableView:heightForRowAtIndexPath:方法设置
3)separatorStyle:属性,返回或设置表格的分隔条样式,它支持UITableViewCellSeparatorStyleNone(无分隔条)和单分隔条的UITableViewCellSeparatorStyleSingleLine以及被蚀刻的单线分隔条UITableViewSeparatorStyleSingleLineEtched这三个枚举值
4)separatorColor:该属性用于设置分隔条的颜色
5)backgroundView:该属性用于返回或设置表格的背景控件,他可以设置一个任意的UIView控件,该控件将被自动缩放到匹配该表
6)tableHeaderView:该属性可设置或返回该表格的页眉控件
7)tableFooterView:该属性可设置或返回表格的页脚控件
8)-numberOfRowsInSection:该方法返回指定分区包含的行数
3、UITableViewDataSource
与UIDatePicker控件类似,UITableView同样只负责最通用的行为,而该控件包含多少个分区,每个分组包含多少表格,各表格对应的UI控件都有UITableViewDataSource提供。因此,UITableView设置UITableViewDataSource对象,并根据需要实现以下方法:
1)-tableView:cellForRowAtIndexPath::必须方法,该方法返回的UITableViewCell对象将作为指定IndexPath对应表格的控件。
2)-tableView:numberO分R哦我是InSection::必须方法,该方法返回的NSInteger值决定指定分区包含的表格行数量
3)-numberOfSectionsInTableView:可选方法,该方法返回的NSInteger值决定该表格所包含的分区数量,如果不实现该方法,默认只包含一个分区
NSIndexPath,它封装了一个表格行的核心信息,包括表格行所在的分区号和行号
4、简单表格
步骤如下:
1)界面布局添加UITableView控件,或通过代码创建一个UITableView对象,并将该对象添加到应用界面中
2)为UITableView设置dataSource属性,该属性值必须是一个实现UITableViewDataSource协议的对象
3)让指定类(通常采用控制器类)实现UITableViewDataSource协议,并实现协议中的两个必须方法。
代码如下:
.h
#import <UIKit/UIKit.h>
@interface FKViewController : UIViewController<UITableViewDataSource>
// 绑定到界面上UITableView控件的IBOutlet属性
@property (strong, nonatomic) IBOutlet UITableView *table;
// 作为UITableView显示数据的两个NSArray
@property (strong, nonatomic) NSArray* books;
@property (strong, nonatomic) NSArray* details;
@end
.m
#import "FKViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface FKViewController ()
@end
@implementation FKViewController
@synthesize books;
@synthesize details;
- (void)viewDidLoad
{
[super viewDidLoad];
// 创建、并初始化NSArray对象。
books = [NSArray arrayWithObjects:@"疯狂Android讲义",
@"疯狂iOS讲义", @"疯狂Ajax讲义" , @"疯狂XML讲义", nil];
// 创建、并初始化NSArray对象。
details = [NSArray arrayWithObjects:
@"长期雄踞各网店销量排行榜榜首的图书",
@"全面而详细的iOS开发图书",
@"Ajax开发图书" ,
@"系统介绍XML相关知识", nil];
// 为UITableView控件设置dataSource
self.table.dataSource = self;
// 为UITableView控件设置页眉控件
self.table.tableHeaderView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:@"tableheader.png"]];
// 为UITableView控件设置页脚控件
self.table.tableFooterView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:@"tableheader.png"]];
}
// 该方法返回值决定各表格行的控件。
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 为表格行定义一个静态字符串作为标示符
static NSString* cellId = @"cellId"; // ①
// 从可重用表格行的队列中取出一个表格行
UITableViewCell* cell = [tableView
dequeueReusableCellWithIdentifier:cellId];
// 如果取出的表格行为nil
if(cell == nil)
{
switch(indexPath.row % 4)
{
case 0:
// 创建一个UITableViewCell对象,使用UITableViewCellStyleSubtitle风格
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:cellId];
break;
case 1:
// 创建一个UITableViewCell对象,使用默认风格
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellId];
break;
case 2:
// 创建一个UITableViewCell对象,使用UITableViewCellStyleValue1风格
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:cellId];
break;
case 3:
// 创建一个UITableViewCell对象,使用UITableViewCellStyleValue2风格
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleValue2
reuseIdentifier:cellId];
break;
}
}
// 从IndexPath参数中获取当前行的行号
NSUInteger rowNo = indexPath.row;
// 取出books中索引为rowNo的元素作为UITableViewCell的文本标题
cell.textLabel.text = [books objectAtIndex:rowNo];
// 将单元格的边框设置为圆角
cell.layer.cornerRadius = 12;
cell.layer.masksToBounds = YES;
// 为UITableViewCell的左端设置图片
cell.imageView.image = [UIImage imageNamed:@"ic_gray.png"];
// 为UITableViewCell的左端设置高亮状态视时的图片
cell.imageView.highlightedImage = [UIImage imageNamed:
@"ic_highlight.png"];
// 取出details中索引为rowNo的元素作为UITableViewCell的详细内容
cell.detailTextLabel.text = [details objectAtIndex:rowNo];
return cell;
}
// 该方法的返回值决定指定分区内包含多少个表格行。
- (NSInteger)tableView:(UITableView*)tableView
numberOfRowsInSection:(NSInteger)section
{
// 由于该表格只有一个分区,直接返回books中集合元素个数代表表格的行数
return books.count;
}
@end
5、UITableViewCell风格
1)UITableViewCellStyleSubtitle:detailTextLabel字体略小,显示在textLabel的下方
2)UITableViewCellStyleDefault:不显示detailTextLabel,只显示textLabel
3)UITableViewCellStyleValue1:detailTextLabel以淡蓝色显示,显示在表格的右边
4)UITableViewCellStyleValue2:textLabel以淡蓝色、略小字体显示,detailTextLabel以大字体显示在表格的右边,不显示image控件
6、访问TableViewCell方法
1)cellForRowAtIndexPath:返回该表格中指定NSIndexPath对应的表格行
2)indexPathForCell:获取该表格中指定表格行对应的NSIndexPath
3)indexPathForRowAtPoint:返回该表格中指定点所在NSIndexPath
4)indexPathsForRowsInRect:返回该表格中指定区域内所有NSIndexPath组成的数组
5)visibleCells:返回该表格中所有可见区域内的表格行组成的数组
6)indexPathsForVisibleRows:返回该表格中所有可见区域内的表格行对应的NSIndexPath组成的数组
7)-scrollToRowAtIndexPath:atScrollPosition:animated:控制该表格滚动到指定NSIndexPath对应的表格行的顶端、中间或下方
8)-scrollToNearestSelectedRowAtScrollPosition:animated::控制该表格滚动到选中表格行的顶端、中间或下方