UI之单个表视图的移动插入和删除
2015-11-15 15:22
274 查看
[btn addTarget:self
action:@selector(doEdit:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem*item = [[UIBarButtonItem
alloc]initWithCustomView:btn];
self.navigationItem.rightBarButtonItem= item;
[self
createTableData];
}
- (void)doEdit:(UIButton*)btn{
self.tableV.editing= !self.tableV.editing;
if (self.tableV.editing) {
[btn setTitle:@"完成"
forState:UIControlStateNormal];
}else{
[btn setTitle:@"编辑"
forState:UIControlStateNormal];
}
}
//点击delete按钮会调用这个方法
- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:(NSIndexPath
*)indexPath{
NSLog(@"编辑");
//先把当前数据源删除
[self.dataArr
removeObjectAtIndex:indexPath.row];
//再刷新tableV
[self.tableV
reloadData];
}
//可以滑动删除可以交换两个行的位置
- (void)tableView:(UITableView*)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath{
NSLog(@"%@ %@",sourceIndexPath,destinationIndexPath);
//交换两个元素的在数据源中的位置
// [self.dataArrexchangeObjectAtIndex:sourceIndexPath.rowwithObjectAtIndex:destinationIndexPath.row];
if(destinationIndexPath.row >sourceIndexPath.row) {//上向下移
//先插入
[self.dataArr
insertObject:self.dataArr[sourceIndexPath.row]
atIndex:destinationIndexPath.row+1];
//后删除
[self.dataArr
removeObjectAtIndex:sourceIndexPath.row];
}else{
[self.dataArr
insertObject:self.dataArr[sourceIndexPath.row]
atIndex:destinationIndexPath.row];
[self.dataArr
removeObjectAtIndex:sourceIndexPath.row+1];
}
//最后刷新
[self.tableV
reloadData];
}
//选中tableView中的某一行时,触发此方法
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
NSLog(@"selected section:%ld row:%ld",(long)indexPath.section,(long)indexPath.row);
}
//某一行被反选时,触发此方法
- (void)tableView:(UITableView*)tableView didDeselectRowAtIndexPath:(NSIndexPath*)indexPath{
NSLog(@"deselect section:%ld row:%ld",(long)indexPath.section,(long)indexPath.row);
}
//设置cell的选中样式,iOS7之前,cell的选中样式默认为蓝色,iOS7之后
cell.selectionStyle=
UITableViewCellSelectionStyleGray;
//设置cell右侧的提示样式
//UITableViewCellAccessoryDisclosureIndicator箭头提示
cell.accessoryType=
UITableViewCellAccessoryDisclosureIndicator;
//将数据,赋值给cell
//indexPathsection带有该行所在的分区信息 row
该行在对应分区的位置
//取到该分区所使用的数组
NSArray*array = [_dataArray
objectAtIndex:indexPath.section];
NSString*str = [array
objectAtIndex:indexPath.row];
//将数组赋值给cell
//UITableViewCellStyleSubtitle副标题才能显示
//设置cell的主标题
cell.textLabel.text = str;
//设置副标题
cell.detailTextLabel.text =
@"test";
//设置图片(头像)
cell.imageView.image = [UIImageimageNamed:@"0.png"];
returncell;
}
//设置分区的头标题
- (NSString *)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{
return [NSString
stringWithFormat:@"第%ld分区头标",'A'+section];
}
//设置分区角标
- (NSString *)tableView:(UITableView*)tableView titleForFooterInSection:(NSInteger)section{
return [NSString
stringWithFormat:@"第%ld分区角标",'A'+section];
}
//编辑调用
- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:(NSIndexPath
*)indexPath{
}
action:@selector(doEdit:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem*item = [[UIBarButtonItem
alloc]initWithCustomView:btn];
self.navigationItem.rightBarButtonItem= item;
[self
createTableData];
}
- (void)doEdit:(UIButton*)btn{
self.tableV.editing= !self.tableV.editing;
if (self.tableV.editing) {
[btn setTitle:@"完成"
forState:UIControlStateNormal];
}else{
[btn setTitle:@"编辑"
forState:UIControlStateNormal];
}
}
//点击delete按钮会调用这个方法
- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:(NSIndexPath
*)indexPath{
NSLog(@"编辑");
//先把当前数据源删除
[self.dataArr
removeObjectAtIndex:indexPath.row];
//再刷新tableV
[self.tableV
reloadData];
}
//可以滑动删除可以交换两个行的位置
- (void)tableView:(UITableView*)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath{
NSLog(@"%@ %@",sourceIndexPath,destinationIndexPath);
//交换两个元素的在数据源中的位置
// [self.dataArrexchangeObjectAtIndex:sourceIndexPath.rowwithObjectAtIndex:destinationIndexPath.row];
if(destinationIndexPath.row >sourceIndexPath.row) {//上向下移
//先插入
[self.dataArr
insertObject:self.dataArr[sourceIndexPath.row]
atIndex:destinationIndexPath.row+1];
//后删除
[self.dataArr
removeObjectAtIndex:sourceIndexPath.row];
}else{
[self.dataArr
insertObject:self.dataArr[sourceIndexPath.row]
atIndex:destinationIndexPath.row];
[self.dataArr
removeObjectAtIndex:sourceIndexPath.row+1];
}
//最后刷新
[self.tableV
reloadData];
}
//选中tableView中的某一行时,触发此方法
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
NSLog(@"selected section:%ld row:%ld",(long)indexPath.section,(long)indexPath.row);
}
//某一行被反选时,触发此方法
- (void)tableView:(UITableView*)tableView didDeselectRowAtIndexPath:(NSIndexPath*)indexPath{
NSLog(@"deselect section:%ld row:%ld",(long)indexPath.section,(long)indexPath.row);
}
//设置cell的选中样式,iOS7之前,cell的选中样式默认为蓝色,iOS7之后
cell.selectionStyle=
UITableViewCellSelectionStyleGray;
//设置cell右侧的提示样式
//UITableViewCellAccessoryDisclosureIndicator箭头提示
cell.accessoryType=
UITableViewCellAccessoryDisclosureIndicator;
//将数据,赋值给cell
//indexPathsection带有该行所在的分区信息 row
该行在对应分区的位置
//取到该分区所使用的数组
NSArray*array = [_dataArray
objectAtIndex:indexPath.section];
NSString*str = [array
objectAtIndex:indexPath.row];
//将数组赋值给cell
//UITableViewCellStyleSubtitle副标题才能显示
//设置cell的主标题
cell.textLabel.text = str;
//设置副标题
cell.detailTextLabel.text =
@"test";
//设置图片(头像)
cell.imageView.image = [UIImageimageNamed:@"0.png"];
returncell;
}
//设置分区的头标题
- (NSString *)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{
return [NSString
stringWithFormat:@"第%ld分区头标",'A'+section];
}
//设置分区角标
- (NSString *)tableView:(UITableView*)tableView titleForFooterInSection:(NSInteger)section{
return [NSString
stringWithFormat:@"第%ld分区角标",'A'+section];
}
//编辑调用
- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:(NSIndexPath
*)indexPath{
}
相关文章推荐
- UI之Tableviewcell
- UI之uiScrollView and uipageControl
- UI之tabBarItem
- UI之把一个控件在屏幕内做任意拖拽并得到时时坐标
- UI之摇晃手机和触摸手势
- UIBarButtonItem改变系统rightbutton和leftbutton的位置
- UI之textField文本框
- Android线程间通信(二):MessageQueue(上)
- UIbutton
- UITabBarController简单介绍
- dojo/io-query源码解析
- jsp Request获取url信息的各种方法比较
- UIColor
- 关于request.getParameterMap()的类型转换和数据获取
- iOS--UIScrollView基本用法和代理方法
- Machine Learning week 2 quiz: Octave Tutorial
- LCS修改版(Longest Common Subsequence 最长公共子序列)
- 基于CefGlue的桌面应用开发
- 使用Easyui pagination分页控件进行分页
- hdoj 4000 Fruit Ninja 【树状数组 + 思维】