UITableView是我们iOS开发中最常用的控件
首先我们先看一下UITableView的继承关系
UITableView 继承于UIScrollView
这里就不多说了 下面我们通过代码来实现UITableView的用法
-(void)tableViewShow
{
/*
UITableView创建的时候我门一般都会写成属性
当我们需要刷新的时候比较方便(个人习惯每次写的时候都喜欢把tableview设置成属性^_^)
[self.tableview reloadData];//刷新
UITableView有两种风格 UITableViewStyleGrouped UITableViewStylePlain
这两者操作起来其实并没有本质区别,只是后者按分组样式显示前者按照普通样式显示而已。
相当于UITableViewStyleGrouped 会有一个分组
*/
self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
self.tableview.delegate = self;//签订协议设置代理人
self.tableview.dataSource = self;
self.tableview.rowHeight = 50;//设置每一行的高度,(不太常用 一般都zai协议方法里面设置cell的高度,那个更加灵活)
//self.tableview.sectionHeaderHeight = 20;//设置section表头的高度
//self.tableview.sectionFooterHeight = 20;//设置section表尾的高度
//添加顶部底部视图
//self.tableview.tableHeaderView = (UIView*)
//self.tableview.tableFooterView =
/*
UITableViewCellSeparatorStyleNone,
UITableViewCellSeparatorStyleSingleLine,
UITableViewCellSeparatorStyleSingleLineEtched
*/
//取消cell与cell的分割线
self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:self.tableview];
}
#pragma mark UITableViewDataSource协议常用方法
//设置分组的个数
-(NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
return 5;
}
//设置cell的个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
//设置右边索引值
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSArray *arr = @[@"a",@"b",@"c"];
return arr;
}
//设置分组的标识
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *str = @"标识";
return str;
}
//创建cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//创建重用标识符
static NSString *tableCell =@"cell";
//UITableViewCell基本上都是自定义的 (在这里就不自定义了 ^_^)
//去重用池中根据标示符取可用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableCell];
//判断是否获取到可用的cell MRC需要autorerelease
if (!cell) {
/*
UITableViewCellStyleDefault, // 左侧显示textLabel(不显示detailTextLabel),imageView可选(显示在最左边)
UITableViewCellStyleValue1, // 左侧显示textLabel、右侧显示detailTextLabel(默认蓝色),imageView可选(显示在最左边)
UITableViewCellStyleValue2, // 左侧依次显示textLabel(默认蓝色)和detailTextLabel,imageView可选(显示在最左边)
UITableViewCellStyleSubtitle // 左上方显示textLabel,左下方显示detailTextLabel(默认灰色),imageView可选(显示在最左边)
*/
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableCell];
}
/*
取消cell的选中状态
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray,
UITableViewCellSelectionStyleDefault NS_ENUM_AVAILABLE_IOS(7_0)
*/
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
//设置tableView的每一行的编辑状态(YES,可编辑)
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//edit按钮的点击事件(当点击edit按钮时触发)
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
}
//当提交编辑操作是触发
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"删除");
}
//设置tableView每一行是否允许移动(YES,可移动)
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//提交移动操作之后触发
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
}
#pragma mark UITableViewDelegate 协议常用方法
//选中cell是触发
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//取消cell的选中状态
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
//设置tableViewCell的编辑样式(插入/删除)
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// UITableViewCellEditingStyleNone,//无
// UITableViewCellEditingStyleDelete,//删除
// UITableViewCellEditingStyleInsert//插入
return UITableViewCellEditingStyleDelete;
}
//设置点击编辑按钮是上面显示的文字,例如 显示删除
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"删除";
}
//设置cell的高度;
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 55.5;
}
//设置section的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
}
//设置cell隔行换色
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath row] % 2 == 0) {
cell.backgroundColor = [UIColor redColor];
} else {
cell.backgroundColor = [UIColor yellowColor];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 50;
}
//设置cell移动的位置
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
NSIndexPath *path = [NSIndexPath indexPathWithIndex:0];
return path;
}