UITableView<二>
2015-09-01 20:26
423 查看
一、tableView编辑
二、tableView移动
三、UITableViewController
编辑的步骤
1.处于编辑状态
![](http://img.blog.csdn.net/20150901200923625)
2、指定TableView那些行可以编辑
![](http://img.blog.csdn.net/20150901201018864)
3、指定tableView编辑的样式
![](http://img.blog.csdn.net/20150901201108035)
4、完成编辑(先操作数据源,再修改UI)
![](http://img.blog.csdn.net/20150901201216618)
UITableView移动
1.让tableView处于编辑状态
![](http://img.blog.csdn.net/20150901201354431)
2、指定TableView哪些行可以移动
![](http://img.blog.csdn.net/20150901201446534)
3、移动完成
![](http://img.blog.csdn.net/20150901201521572)
4、监测移动过程
![](http://img.blog.csdn.net/20150901201606633)
UITableViewController
UITableViewController继承自UIViewController,自带一个tableView
self.view不是UIView而是UITableView
datasource和delegate默认都是self(UITableViewController)
开发中只需要建立UITableViewController子类
代码示例
二、tableView移动
三、UITableViewController
编辑的步骤
1.处于编辑状态
2、指定TableView那些行可以编辑
3、指定tableView编辑的样式
4、完成编辑(先操作数据源,再修改UI)
UITableView移动
1.让tableView处于编辑状态
2、指定TableView哪些行可以移动
3、移动完成
4、监测移动过程
UITableViewController
UITableViewController继承自UIViewController,自带一个tableView
self.view不是UIView而是UITableView
datasource和delegate默认都是self(UITableViewController)
开发中只需要建立UITableViewController子类
代码示例
//1.让tableView 处于可编辑的状态 - (void)setEditing:(BOOL)editing animated:(BOOL)animated { [super setEditing:editing animated: animated]; //设置tableView的编辑状态 [_tableView setEditing:editing animated:animated]; } //2.设置指定分区section中的行(row)是否可以被编辑 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0 && indexPath.row == 0) { return NO; } return YES; } //3.设置指定分区(section)中的行(row)是什么类型的编辑样式(删除/插入) - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { return UITableViewCellEditingStyleInsert; } return UITableViewCellEditingStyleDelete; } //4.编辑完成(先操作数据,然后再操作UI) - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { //删除的操作 //①.先操作数据 NSString * key = _keysArray[indexPath.section]; NSMutableArray * array = _AllCityDic[key]; [array removeObjectAtIndex:indexPath.row ]; //②.再操作UI [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; if (array.count == 0 ) { //①.先操作数据 [_keysArray removeObject:key]; [_AllCityDic removeObjectForKey:key]; //②.再操作UI NSIndexSet * indexSet = [NSIndexSet indexSetWithIndex:indexPath.section]; [tableView deleteSections: indexSet withRowAnimation:UITableViewRowAnimationRight ]; } }else if (editingStyle == UITableViewCellEditingStyleInsert) { //添加操作 //①.先操作数据 //创建需要添加的数据对象 City * city = [[City alloc ]init]; city.name = @"丰台区"; city.area = @"30万"; //找到要添加的位置 NSString * key = _keysArray[indexPath.section]; NSMutableArray * cityArray = _AllCityDic[key]; [cityArray insertObject:city atIndex:indexPath.row]; //②.再操作UI [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight]; } } #pragma mark----UITableView //1.让tableView处于可编辑状态(同上) //2.设置指定的section中的行row可以被移动 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } //3.实现移动 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { //获取数组中要被移动的元素 // 1.获取key NSString * key = _keysArray[sourceIndexPath.section]; //2.通过key获取对应的城市数组 NSMutableArray * citysArray = _AllCityDic[key]; //3.从citysArray 中获取,你要移动的那个城市对象 City * city = citysArray[sourceIndexPath.row]; //4.给city的引用计数+1,是为了防止野指针访问 [city retain]; //5.删除sourceIndex.row对应数组的城市对象 [citysArray removeObjectAtIndex:sourceIndexPath.row ]; //6.把要移动的城市对象,移动到destinationIndexPath.row的那个位置 [citysArray insertObject:city atIndex:destinationIndexPath.row]; //7.遵循内存管理法则 [city release]; } //4.限制跨区移动 - (NSIndexPath* )tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath { //如果你最终移动到的位置和你现在的位置在同一个区 if(sourceIndexPath.section == proposedDestinationIndexPath.section ) { return proposedDestinationIndexPath; } //否则回到原位 return sourceIndexPath ; } //当选中某一行时,推出下一级页面 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { FirstTableViewController * firstTVC = [[FirstTableViewController alloc]initWithStyle:UITableViewStylePlain]; //属性传值 //获取字典中,要传过去的值 NSString * key = _keysArray[indexPath.section ]; NSMutableArray * citysArray = _AllCityDic[key]; City * city = citysArray[indexPath.row]; firstTVC.city = city; [self.navigationController pushViewController:firstTVC animated:YES]; [firstTVC release]; }
相关文章推荐
- UIView动画和CAAnimation动画
- 页面之间传值方式的总结,五种方式,通知,block,代理,单例,NSUERDEFALUT,
- Android UI设计小知识——渐变色背景的制作
- HDU 5014 Number Sequence
- UITableView <一>
- UIView动画 防多星球环绕转动
- Android UI设计小知识——按钮背景的制作
- FragmentTabHost切换Fragment时避免UI重新加载
- POJ 3368 Frequent values RMQ ST算法/线段树
- uestc893
- UICollectionView基础
- empty返回值为true的三种情况
- UVA 1612 Guess
- 559B - Equivalent Strings & 560D - Equivalent Strings
- UVa 11572 - Unique Snowflakes (滑动窗口STL_map||set) 好题!!!
- UITableView,UICollectionView,UIScrollView快速返回顶部
- UITableView,UICollectionView,UIScrollView快速返回顶部
- Android 教你打造炫酷的ViewPagerIndicator 不仅仅是高仿MIUI
- Android UI设计小知识——Nine-Patch图片
- 黑马程序员——Foundation框架入门:NSNumber,NSValue和NSDate