您的位置:首页 > 移动开发 > IOS开发

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];

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];


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: