[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{
}