iOS表分区头和表视图的编辑模式
2015-11-30 12:51
447 查看
设置分区头信息
-(NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section
设置分区头视图
-(UIView )tableView:(UITableView )tableView viewForHeaderInSection:(NSInteger)section
表视图分区头到顶部时仍然存在,
TableView的style应该为Plain
-(NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section{
return @“分区头”;
}
设置表头视图
UIImageView *headerImageView = [[UIImageView alloc]init];
headerImageView.frame = CGRectMake(0, 0, 0, 0);
headerImageView.image = [UIImage imageNamed:@“xxx.jpg”];
self.tableView.tableHeaderView = headerImageView;
表视图的编辑模式需要配置导航栏右侧的编辑按钮
可以使用系统版的editButtonItem来配置导航栏的右侧按钮,但文字是英文版
self.navigationItem.rightBarButtonItem = self.editButtonItem;
还可以自定义
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@”编辑” style:UIBarButtonItemStylePlain target:self action:@selector(clickEditButton:)];
点击编辑按钮,切换表格的编辑模式
-(void)clickEditButton:(UIBarButtonItem *)item
{
[self.tableView setEditing:!self.tableView.editing animated:YES];
}
表格的编辑功能
问1:当前行是否可编辑
-(BOOL)tableView:(UITableView )tableView canEditRowAtIndexPath:(NSIndexPath )indexPath
{
if (indexPath.row == 0) {
return NO;
}else{
return YES;
}
}
问2:当前行的编辑样式是什么
-(UITableViewCellEditingStyle)tableView:(UITableView )tableView editingStyleForRowAtIndexPath:(NSIndexPath )indexPath
{
if (indexPath.row == self.allCitys.count-1)
{
return UITableViewCellEditingStyleInsert;
}else{
return UITableViewCellEditingStyleDelete;
}
//return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;//可以多选
}
//答1:点击编辑按钮后做什么
-(void)tableView:(UITableView )tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath )indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete){//点击的是删除按钮
// 1.先删除行所在的数据模型中的数据
[self.allCitys removeObjectAtIndex:indexPath.row];
}
行的移动
//问1:改行是否可以移动
-(BOOL)tableView:(UITableView )tableView canMoveRowAtIndexPath:(NSIndexPath )indexPath
{
return YES;
}
//答1:移动后做什么
-(void)tableView:(UITableView )tableView moveRowAtIndexPath:(NSIndexPath )sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//1.按照旧的位置,找到原始的数据
City *city = self.allCitys[sourceIndexPath.row];
}
-(NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section
设置分区头视图
-(UIView )tableView:(UITableView )tableView viewForHeaderInSection:(NSInteger)section
表视图分区头到顶部时仍然存在,
TableView的style应该为Plain
-(NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section{
return @“分区头”;
}
设置表头视图
UIImageView *headerImageView = [[UIImageView alloc]init];
headerImageView.frame = CGRectMake(0, 0, 0, 0);
headerImageView.image = [UIImage imageNamed:@“xxx.jpg”];
self.tableView.tableHeaderView = headerImageView;
表视图的编辑模式需要配置导航栏右侧的编辑按钮
可以使用系统版的editButtonItem来配置导航栏的右侧按钮,但文字是英文版
self.navigationItem.rightBarButtonItem = self.editButtonItem;
还可以自定义
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@”编辑” style:UIBarButtonItemStylePlain target:self action:@selector(clickEditButton:)];
点击编辑按钮,切换表格的编辑模式
-(void)clickEditButton:(UIBarButtonItem *)item
{
[self.tableView setEditing:!self.tableView.editing animated:YES];
item.title = self.tableView.editing?@"完成":@"编辑";
}
表格的编辑功能
问1:当前行是否可编辑
-(BOOL)tableView:(UITableView )tableView canEditRowAtIndexPath:(NSIndexPath )indexPath
{
if (indexPath.row == 0) {
return NO;
}else{
return YES;
}
}
问2:当前行的编辑样式是什么
-(UITableViewCellEditingStyle)tableView:(UITableView )tableView editingStyleForRowAtIndexPath:(NSIndexPath )indexPath
{
if (indexPath.row == self.allCitys.count-1)
{
return UITableViewCellEditingStyleInsert;
}else{
return UITableViewCellEditingStyleDelete;
}
//return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;//可以多选
}
//答1:点击编辑按钮后做什么
-(void)tableView:(UITableView )tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath )indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete){//点击的是删除按钮
// 1.先删除行所在的数据模型中的数据
[self.allCitys removeObjectAtIndex:indexPath.row];
// 2.更新tableView的显示 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }else{//点击的是增加按钮 // 1.先将数据增加到数据模型中 City *city = [[City alloc]init]; city.name = @"test"; city.population = 1000; [self.allCitys addObject:city]; // 2.刷新表格 NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:self.allCitys.count-1 inSection:0]; [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }
}
行的移动
//问1:改行是否可以移动
-(BOOL)tableView:(UITableView )tableView canMoveRowAtIndexPath:(NSIndexPath )indexPath
{
return YES;
}
//答1:移动后做什么
-(void)tableView:(UITableView )tableView moveRowAtIndexPath:(NSIndexPath )sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//1.按照旧的位置,找到原始的数据
City *city = self.allCitys[sourceIndexPath.row];
//2.将数据从原位置上移除 [self.allCitys removeObjectAtIndex:sourceIndexPath.row]; //3.再将数据按照新的位置插入回数组 [self.allCitys insertObject:city atIndex:destinationIndexPath.row];
}
相关文章推荐
- iOS文件操作
- iOS表视图下拉刷新与索引
- iOS搜索框
- iOS沙盒
- 从头来之【iOS及历史版本特性介绍】
- IOS消息推送
- iOS归档 反归档 Archiving Unarchiver
- iOS多线程技术
- IOS 截屏模糊 的问题
- iOS开发之数据存储(一)
- iOS开发--常见坑(横竖屏问题)
- 推荐一个iOS关于颜色的库-Wonderful
- iOS 将图片等比例缩放
- [IOS]SMS获取&验证手机验证码
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
- iOS开发之数据传递(一)
- ios审核加急,怎么快速上架
- iOS开发免费API接口
- iOS图片编辑(CIFilter)
- ios 开发日记 23 - Reveal使用步骤