UITableView 表视图, 是iOS中最重要也是在开发过程中必须使用的控件.UITableView继承于UIScrollView,但UITableView只支持竖直滑动.
UITableView的基本属性与方法
//1.创建一个UITableView对象,大小为屏幕大小
UITableView *tableV = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style: UITableViewStylePlain];
//2.将UITableView放到self.view
//2.1 设置UItableView的背景色
tableV.backgroundColor = [UIColor cyanColor];
//2.2 设置UItableView的分割线颜色
tableV.separatorColor = [UIColor redColor];
//2.3 设置分割线的样式
// tableV.separatorStyle = UITableViewCellAccessoryDetailButton;
//2.4 设置UITableView的行高
// tableV.rowHeight = 80;
[self.view addSubview:tableV];
//2.5设置UITableView的代理
//2.5.1 数据源代理
tableV.dataSource = self;
//2.5.2 代理
tableV.delegate = self;
//3.释放所有权
[tableV release];
UITableViewDataSource 提供配置UITableView的一些数据信息
UITableViewDelegate 提供一系列UITableView的触发时机
#pragma mark - UITableViewDataSource
//section:分区(组) row 行(单元格)
//返回每个分区都有多少行 tabView默认有一个分区
//该方法在tableView加载完毕之后不会再触发
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
#waring 假数据
return 20;
}
//根据行返回对应的UITableView对象
//当系统(屏幕)需要显示cell时就会触发该方法.所以在该方法中直接创建UITableViewCell是特别消耗内存的,所有UITableView对UITableViewCell提供重用机制.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//创建UITableViewCell并返回
// UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil] autorelease];
#warning UITableView重用写法1
//该方法会通过indentifier从字典中取出对应的set,然后通过anyObject方法,从集合中取重用的Cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"set"];
//如果重用池中没有可用的Cell,在创建一个,并附上重用标识,以便其滑出屏幕之后可以放到对应的set中,该方法要先在viewdidload里面注册cell,并附上标识符([
[self.tableView registerClass:[ContactCell class] forCellReuseIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"set"];
}
/**
* 1. UITableViewCellStyleDefault
只显示imageView textLabel ,辅助视图, detailTextLabel属性即使设置也不显示.
2.UITableViewCellStyleValue1
可以显示mageView textLabel ,辅助视图, detailTextLabel
3.UITableViewCellStyleValue2
只显示textLabel(右对齐) ,辅助视图, detailTextLabel(左对齐),不显示imageView
4.UITableViewCellStyleSubtitle
可以显示mageView textLabel ,辅助视图, detailTextLabel;textLabel和 detailTextLabel都在左侧,而且
detailTextLabel 在textLabel下面
*/
// cell.textLabel.text = @"哈哈哈";
// cell.detailTextLabel.text = @"呵呵呵呵";
// NSLog(@"%d", ++count);
cell.imageView.image = [UIImage imageNamed:@"1.jpg"];
//设置UITableView的辅助视图样式
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
#warning contentView是UITableView的内容视图,imageView,textLabel,detailTextLabel都在其上面,所以以后如果我们自定义UITableViewCell,所有的子控件需要添加到contentView上
// cell.contentView.backgroundColor = [UIColor redColor];
// NSLog(@"%ld %ld", indexPath.section, indexPath.row);
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 4;
}
//设置header,fotter的title
/**
当TableView的类型为plain时,其页眉页脚不会跟随TableView滑动,直到对应的分区划出屏幕才会跟随滑出 页眉页脚的title与设置的完全一样
当TableView的类型为group时,其页眉页脚跟随TableView滑动 页眉的title会自动转化成大写,页脚与设置完全一样
*/
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (section == 0) {
return @"A";
}else{
return @"B";
}
}
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
if (section == 0) {
return @"C";
}else{
return @"D";
}
}
#pragma mark - UITableDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
return 60;
}else{
return 100;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 80;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 50;
}
//自定义页眉与页脚
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *header = [[UIView alloc] init];
header.backgroundColor = [UIColor redColor];
return [header autorelease];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *fotter = [[UIView alloc] init];
fotter.backgroundColor = [UIColor greenColor];
return [fotter autorelease];
}
//点击cell触发的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"%ld %ld", indexPath.section, indexPath.row);
}
//cell被取消选中状态时触发
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%ld %ld", indexPath.section, indexPath.row);
}
//设置TableView右边的引导
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return @[@"A", @"B", @"C", @"D"];
}