您的位置:首页 > 产品设计 > UI/UE

UITableView<二>

2015-09-01 20:26 423 查看
一、tableView编辑

二、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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: