//创建一个UITableView(表视图):通常用来显示相同数据结构的大量数据.
表示图的两种样式:
UITableViewStylePlain(普通样式)
UITableViewStyleGrouped(分组样式,更加常用)
//设置tableView的分割线样式属性:sepratorStyle
共三种:
UITableViewCellSeparatorStyleNone, 无样式
UITableViewCellSeparatorStyleSingleLine, 单线样式
UITableViewCellSeparatorStyleSingleLineEtched 这种样式只在group样式中存在
//设置tableView的分割线的颜色属性:separatorColor
firstTableView.separatorColor = [UIColorredColor];
//设置tableView的单元格高度属性:rowHeight
firstTableView.rowHeight =100;
//设置数据源的代理人
两种代理方法
UITableViewDataSource
数据源代理,代理者必须执行两种代理方法第一种: 返回当前分区中的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection(NSInteger)section;
第二种:返回单元格
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
该方法有cell的重用机制;
//创建一个重用标识
static NSString *cellIndentify =@"cellIndentify";
//第一步:去当前tableView的重用池中取闲置的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentify];
//第二部:如果重用池中,未找到闲置的cell(也就是cell为nil);那么创建一个UITableViewCell;
if(!cell){
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIndentify];
}
cell.backgroundColor 设置单元格背景颜色
cell.textLabel.text设置单元格中的文字
cell.imageView.image为单元格添加图片
cell.selectionStyle 指定选中效果
cell.detailTextLabel.text 设置单元格标签,描述单元格
其他代理方法
设置有多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
设置分区尾文字
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
是否能编辑分区中的行
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
是否能移动分区中的行
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
返回右侧索引栏
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
UITableViewDelegate 表示图代理方法
点击单元格后执行方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
对TableView里的单元格进行编辑
#pragma mark 编辑重写
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
[supersetEditing:editing animated:animated];
if(editing){
self.navigationItem.rightBarButtonItem.title =@"完成";
#pragma mark 第一步:使tableView处于编辑状态
[self.tableView setEditing:YES animated:YES];
}else{
self.navigationItem.rightBarButtonItem.title =@"编辑";
#pragma mark 最后一步:使tableView退出状态
[self.tableView setEditing:NO animated:YES];
}
}
#pragma mark 第二步:指定某行是否可编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
//全部可以编辑
return YES;
}
#pragma mark 第三部:指定某行的编辑样式(三种样式:None,delete,insert)
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCellEditingStyleDelete; 删除样式
UITableViewCellEditingStyleInsert; 插入样式
}
#pragma mark 第四部:提交编辑
/**
* 参数说明
*
* @param tableView 当前tableView
* @param editingStyle 当前行编辑样式
* @param indexPath 当前行的标识
*/
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//判断当前的编辑样式
if(editingStyle ==UITableViewCellEditingStyleDelete){
//进行删除操作
#warning 注意:先操作数据,在更新UI
//删除数据
[[self.sectionsArrayobjectAtIndex:indexPath.section]removeObjectAtIndex:indexPath.row];
//删除单元格
[self.tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft];
}else{
[[self.sectionsArray objectAtIndex:indexPath.section]insertObject:@"王菲"atIndex:indexPath.row];
//插入单元格
[self.tableViewinsertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
}
}
#pragma mark 移动第二步:指定某行是否可以移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
#pragma mark 移动第三步:移动完成
/**
* 参数说明
*
* @param tableView 1.当前tableView
* @param sourceIndexPath 2.当前拖动的单元格
* @param destnationIndexPath 3.目的位置
*/
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{ //交换数据
[[self.sectionsArrayobjectAtIndex:sourceIndexPath.section]exchangeObjectAtIndex:sourceIndexPath.rowwithObjectAtIndex:destinationIndexPath.row];
//更改UI
[self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
#warning 非常常用:刷新tableView数据
//[self.tableView reloadData];
#warning 偶尔使用:刷新指定行数据(如果当前tableView数据比较大,并且并不需要全部刷新,会使用到指定刷新)
// [self.tableView reloadRowsAtIndexPaths:<#(NSArray *)#> withRowAnimation:<#(UITableViewRowAnimation)#>];
}
#pragma mark 限制跨区移动
/**
* 参数说明
*
* @param tableView 1.当前tableView
* @param sourceIndexPath 2.当前拖动单元格所在位置
* @param proposedDestinationIndexPath 3.计划要到的单元格所在位置
*
* @return <#return value description#>
*/
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
//判断拖动的单元格和计划要到的单元格是否为同一分区
if(sourceIndexPath.section == proposedDestinationIndexPath.section){
return proposedDestinationIndexPath;
}
else{
//如果不在一个分区,那么返回原位置
return sourceIndexPath;
}
}