UI之tableViewEdit(删除,添加,插入)
来源:互联网 发布:网络电视机不用机顶盒 编辑:程序博客网 时间:2024/06/07 23:46
self.navigationItem.title = @"笑笑";
self.tableView = [[UITableView alloc]initWithFrame: self.view.frame style:UITableViewStylePlain];//设置数据源代理
self.tableView.dataSource = self;
//设置代理
self.tableView.delegate = self;
self.tableView.backgroundColor = [UIColor yellowColor];
self.tableView.separatorColor = [UIColor cyanColor];
[self.view addSubview:self.tableView];
NSMutableArray *group1 = [NSMutableArray arrayWithObjects:@"天亮了",@"明天,你好",@"God is a girl", nil];
NSMutableArray *group2 = [NSMutableArray arrayWithObjects:@"流行",@"现代",@"DJ", nil];
NSMutableArray *group3 = [NSMutableArray arrayWithObjects:@"民族",@"爵士",@"街舞", nil];
self.array = [[NSMutableArray alloc]initWithObjects:group1,group2,group3, nil];
//在导航栏右边添加编辑按钮
self.navigationItem.rightBarButtonItem = self.editButtonItem;
//在导航栏左边添加编辑完成按钮
UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(finish)];
self.navigationItem.leftBarButtonItem = left;
}
#pragma mark-----UITableViewDataSource
//返回每个分区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//取出对应分区下边小组
NSMutableArray *group = self.array[section];
return group.count;
}
//返回多少个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.array.count;
}
//cell显示的位置及内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//创建一个静态全局标量,--重用标示符
static NSString *identifier = @"Cycle";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//设置背景颜色
cell.backgroundColor = [UIColor magentaColor];
//判断cell是否为空,如果为空表示重用队列里没有可用cell
if (cell == nil) {
//cell为空时重新创建
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
//取出当前分区的小组
NSMutableArray *group = self.array[indexPath.section];
cell.textLabel.text = group[indexPath.row];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (section == 0) {
return @"music";
}else if (section == 1){
return @"song";
}else if (section == 2){
return @"dance";
}
return nil;
}
#pragma mark -----TableView可以编辑
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
//第一步:让当前的tableView处于可编辑状态
[self.tableView setEditing:YES animated:YES];
}
//第二步:让所有cell处于可编辑状态
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//第三步:选择tableView的编辑样式(添加或者删除)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//选择删除和添加时更换return返回内容,插入和添加都用UITableViewCellEditingStyleInsert
// return UITableViewCellEditingStyleDelete;
return UITableViewCellEditingStyleInsert;
}
//第四步:编辑完成,提交编辑状态(先操作数据源,后更新UI)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//找到对应分区的
NSMutableArray *group = self.array[indexPath.section];
//delete
//删除对应分区数组里对应位置的元素
//[group removeObjectAtIndex:indexPath.row];
////通知tableView删除对应行
//[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//add
//在tableView指定位置插入元素
[group insertObject:@"你懂得" atIndex:indexPath.row];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (void)finish{
[self.tableView setEditing:NO animated:YES];
}
#pragma mark-----TableView移动方法
//第一步:让tableView处于可编辑状态
//第二步:指定tableView哪一行可以编辑
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;//让所有cell都可以移动
}
//第三步:移动完成
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
//获取元素所在数组
NSMutableArray *array = self.array[sourceIndexPath.section];
//把当前数组中对应位置的元素取出来,指向将要被删除的元素,如果继续访问可能有潜在的风险,添加retain持有这个对象一份
NSString *name = [array[sourceIndexPath.row] retain];
//把元素从原来位置上删除
[array removeObjectAtIndex:sourceIndexPath.row];
//插入到新的位置
[array insertObject:name atIndex:destinationIndexPath.row];
[name release];
}
//监测一定过程禁止跨区域移动
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
//判断起始位置的区域跟将要移动目的地区域是否为一个区域
if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
return proposedDestinationIndexPath;
}//从哪来回哪去
return sourceIndexPath;
}
1 0
- UI之tableViewEdit(删除,添加,插入)
- UI之单个表视图的移动插入和删除
- DOM常用方法之 插入,删除,添加等。
- 动态添加、插入、删除菜单
- java 单链表 添加 插入 删除
- VC combobox 的使用(添加、删除、插入等)
- VC combobox 的使用(添加、删除、插入等)
- Snail—UI学习之表视图TableView单行添加、删除和移动
- IOS--UI--LessonUITableViewEdting 编辑 添加 删除
- 十二、UI-Grid 添加和删除列
- 添加、删除、插入和替换[文本节点]
- 支持插入、添加、删除的PyGridTableBase子类
- 行式填报表自定义插入添加删除按钮
- 动态添加菜单\删除菜单\插入菜单
- 创建,添加,插入和删除DOM元素
- mysql 建表 插入数据 添加删除主外键
- 列表的添加、插入、删除、切割、合成
- 队列的 插入 删除 添加操作
- Sql : ceiling floor Round 小数位
- Alcatraz的安装和使用
- GridControl之GridView(属性篇)(一)
- LeetCode 110:Balanced Binary Tree
- 设定MyEclipse编辑代码区域文字的大小及非关键字的字体、字形和颜色
- UI之tableViewEdit(删除,添加,插入)
- 计算机数据包和帧
- 栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈栈
- 范式学习
- 链表222222222222222
- Eclipse或者MyEclipse—在Eclipse或MyEclipse中的操作(3)
- 【Ogre引擎架构】 第十讲 唯美场景-天空盒SkyBox
- Objective-C Blocks Caveat
- MySQL和SQL入门(1-5)