IOS UITableView 多选,全选删除,拖动,置顶的实现
来源:互联网 发布:淘宝潮牌男模特排名 编辑:程序博客网 时间:2024/06/08 16:46
[self.tableView setEditing:YES animated:NO];
2.设置编辑样式为多选(两个属性加起来就是这么神奇)
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath
{
return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;
}
0 处理全选 //实现点击全选按钮,实现全部选中,再点击删除,删除所有选中的cell
//暂时没做反选
//全选按钮点击事件
-(void)allChoiceBtnClick:(UIButton *)btn
{
btn.selected = ! btn.selected;
if (btn.selected) {
//将可见的设为全选状态
for (NSIndexPath *cellIndex in [self.tableView indexPathsForVisibleRows])
{
KeChooseManagerCell * otherCell = (KeChooseManagerCell *)[self.tableViewcellForRowAtIndexPath:cellIndex];
otherCell.selected = YES;
self.isAllSelected = YES;
self.indexArr = [NSMutableArray new];
//遍历数组创建含有section和row的NSIndexPath,然后将它传给tableview,用来删除全部选中的cell
for (int i = 0; i < self.stockList.count; i++) {
NSIndexPath * realIndex = [NSIndexPath indexPathForRow:i inSection:0];
[self.indexArr addObject:realIndex];
}
}
}else{
for (NSIndexPath *cellIndex in [self.tableView indexPathsForVisibleRows])
{
KeChooseManagerCell * otherCell = (KeChooseManagerCell *)[self.tableViewcellForRowAtIndexPath:cellIndex];
otherCell.selected = NO;
self.isAllSelected = NO;
}
}
}
//删除点击处理
-(void)deleteBtnClick:(UIButton *)btn
{
if (self.isAllSelected) {//全选
[self.stockList removeAllObjects];
[self.tableView beginUpdates];
KELog(@"allKeys %@",[deleteDic allKeys]);
KELog(@"stockcount %d",self.stockList.count);
KELog(@"stock %@",self.stockList);
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithArray:self.indexArr] withRowAnimation:UITableViewRowAnimationFade];
KELog(@"allValues %@",[deleteDic allValues]);
// [deleteDic removeAllObjects];
[self.tableView endUpdates];
[self.tableView reloadData];
newDataList = self.stockList;
}else
{//非全选
//动态绑定(运行时有效<不是编译时起作用>)
__block NSMutableArray * temp = [NSMutableArray new];
__block typeof(self) weakSelf = self;
[indexArray enumerateObjectsUsingBlock:^(ChooseStockInfo * obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSIndexPath * indexPath = objc_getAssociatedObject(obj, &kKeyForindexAndModel);
[temp addObject:indexPath];
[weakSelf.stockList removeObject:obj];
}];
[self.tableView beginUpdates];
KELog(@"allKeys %@",[deleteDic allKeys]);
KELog(@"stockcount %d",self.stockList.count);
KELog(@"stock %@",self.stockList);
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithArray:temp] withRowAnimation:UITableViewRowAnimationFade];
KELog(@"allValues %@",[deleteDic allValues]);
// [deleteDic removeAllObjects];
[self.tableView endUpdates];
[self.tableView reloadData];
newDataList = self.stockList;
}
//选中
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ChooseStockInfo * model = [self.stockList objectAtIndex:indexPath.row];
[indexArray addObject:model];
//绑定model和indexPath
objc_setAssociatedObject(model, &kKeyForindexAndModel, indexPath, OBJC_ASSOCIATION_RETAIN);
KELog(@"didSelect %@",deleteDic);
}
//取消选中
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[deleteDicremoveObjectForKey:[self.stockListobjectAtIndex:indexPath.row]];
[indexArray removeObject:indexPath];
KELog(@"Deselect %@",deleteDic);
}
1.拖动
//设置可拖动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
//拖动之后处理的业务逻辑- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
//更新沙盒里的自选股字符串数组
NSArray * array = (NSArray *)[[SandBox shareInstance] getCoreInfoFromSandBoxWithKey:stockListSandBox];
stockStrLists = [NSMutableArray arrayWithArray:array];
//向上拖动
if (fromIndexPath.row > toIndexPath.row) {
[self.stockList insertObject:[self.stockList objectAtIndex:fromIndexPath.row] atIndex:toIndexPath.row];
[self.stockList removeObjectAtIndex:fromIndexPath.row+1];
//到哪一行
[tableView reloadData];
//string
[stockStrLists insertObject:[stockStrLists objectAtIndex:fromIndexPath.row] atIndex:toIndexPath.row];
[stockStrLists removeObjectAtIndex:fromIndexPath.row+1];
newDataList = self.stockList;
[[SandBox shareInstance] saveCoreInfoToSandBox:stockStrLists withKey:stockListSandBox];
self.changed = YES;
[[NSNotificationCenter defaultCenter] postNotificationName:kStockSortChangedNotification object:@{@"stockList":newDataList,@"operationType":@(Sort),@"stockStrList":stockStrLists}];
}else//向下拖动
{
[self.stockList insertObject:[self.stockList objectAtIndex:fromIndexPath.row] atIndex:toIndexPath.row];
[self.stockList removeObjectAtIndex:fromIndexPath.row - 1];
[tableView reloadData];
[stockStrLists insertObject:[stockStrLists objectAtIndex:fromIndexPath.row] atIndex:toIndexPath.row];
[stockStrLists removeObjectAtIndex:fromIndexPath.row - 1];
newDataList = self.stockList;
[[SandBox shareInstance] saveCoreInfoToSandBox:stockStrLists withKey:stockListSandBox];
self.changed = YES;
[[NSNotificationCenter defaultCenter] postNotificationName:kStockSortChangedNotification object:@{@"stockList":newDataList,@"opertationType":@(Sort),@"stockStrList":stockStrLists}];
}
}
[cell setTopBlcok:^(){
KELog(@"--- %ld ----",(long)indexPath.row);
//插入到第一个
[self.stockListinsertObject:[self.stockListobjectAtIndex:indexPath.row]atIndex:0];
ChooseStockInfo * aa = (ChooseStockInfo *)[self.stockListobjectAtIndex:indexPath.row];
KELog(@"dataList %@",aa.stockName);
//删除挪到下一行的元素
[self.stockListremoveObjectAtIndex:indexPath.row +1];
[tableView reloadData];
newDataList =self.stockList;
self.changed =YES;
KELog(@"newDataListTop %@",newDataList);
KELog(@"stockStrLists begin %@",stockStrLists);
NSArray * array = (NSArray *)[[SandBoxshareInstance] getCoreInfoFromSandBoxWithKey:stockListSandBox];
stockStrLists = [NSMutableArrayarrayWithArray:array];
[stockStrListsinsertObject:[stockStrListsobjectAtIndex:indexPath.row]atIndex:0];
[stockStrLists removeObjectAtIndex:indexPath.row + 1];
KELog(@"stockStrLists end %@ indexrow%ld",stockStrLists,(long)indexPath.row);
[[SandBox shareInstance] saveCoreInfoToSandBox:stockStrListswithKey:stockListSandBox];
[[NSNotificationCenterdefaultCenter] postNotificationName:kStockSortChangedNotificationobject:@{@"stockList":newDataList,@"operationType":@(Sort),@"stockStrList":stockStrLists}];
KELog(@"newData %@",newDataList);
}];
- IOS UITableView 多选,全选删除,拖动,置顶的实现
- iOS中UITableview实现跨区域拖动效果的实现
- IOS之UITableView划动删除的实现
- IOS 如何实现UITableView的划动删除
- ios UITableView划动删除的实现
- [IOS]UITableView实现Cell的滑动删除
- iOS UITableView实现滑动删除、增加、多选
- iOS UITableView的细致功能(滑动删除,拖动排序,添加行,搜索过滤)
- iOS中使用UITableView的实现向左拉实现删除
- UITableView实现全选
- iOS UItableView+UINavigationController实现单元格的删除
- iOS UItableView+UINavigationController实现单元格的删除
- iOS开发:UITableView实现侧滑删除cell的功能
- UITableView划动删除的实现
- UITableView划动删除的实现
- UITableView划动删除的实现
- UITableView滑动删除的实现
- UITableView划动删除的实现
- python之自动生成C++的父子双向继承关系
- UESTC 1073 单点更新
- Codeforces 596D 区间DP
- Ubuntu 14.04数据库服务器--mysql的安装和配置
- Android Linux 常用命令
- IOS UITableView 多选,全选删除,拖动,置顶的实现
- XML文件配置使用
- 2048游戏优化,1小时,屏幕适配相关
- 保存文件到手机内存
- Android中视图重绘的方法
- 状态模式(State Pattern)
- ubuntu14.04,以root身份登入桌面!!
- c++ 11 多线线程系列-----------使用c++11 lambda创建线程
- ref与out之间的区别