标签:ios
//
// RootViewController.m
// TableViewEdit
//
// Created by Duba on 15-5-07.
// Copyright (c) 2015年 Dubai. All rights reserved.
//
#import "RootViewController.h"
@interface RootViewController ()
{
NSMutableArray *_dataArray;
UITableView *_tableView;
NSMutableArray *_arr1;
}
@end
@implementation RootViewController
- (void)dealloc
{
[_dataArray release];
[_tableView release];
[super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//亚洲
_arr1 = [NSMutableArray arrayWithObjects:@"中国",@"新加坡",@"蒙古", nil];
//欧洲
NSMutableArray *arr2 = [NSMutableArray arrayWithObjects:@"美国",@"加拿大",@"巴西", nil];
//欧洲
NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"英国",@"法国",@"德国", nil];
//把三个州的小数组添加到大数组里
_dataArray = [[NSMutableArray alloc]initWithObjects:_arr1,arr2,arr3, nil];
// //加一个按钮
// UIBarButtonItem *edite = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:(UIBarButtonItemStylePlain) target:self action:@selector(didClickEditButtonitem:)];
// self.navigationItem.rightBarButtonItem = edite;
// [edite release];
//使用controller自带的编辑buttonitem,直接用不用创建对象
self.navigationItem.rightBarButtonItem = self.editButtonItem;//自带一个属性
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStyleGrouped)];
[self.view addSubview:_tableView];
_tableView.dataSource = self;
_tableView.delegate = self;
_tableView.separatorColor = [UIColor greenColor];
//self.editButtonItem.title = @"编辑";
}
//controller的重写方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
//NSLog(@"%d",editing);
[_tableView setEditing:editing animated:animated];
self.editButtonItem.title = editing?@"完成":@"编辑";
}
////按钮响应,进入编辑状态
//- (void)didClickEditButtonitem:(UIBarButtonItem *)buttonitem
//{
//
// //tableView进入编辑状态
// if ([buttonitem.title isEqualToString:@"编辑"]) {
// [_tableView setEditing:YES animated:YES];
// buttonitem.title = @"完成";
// }else{
//
// [_tableView setEditing:NO animated:YES];
// buttonitem.title = @"编辑";
//
// }
// //[_tableView setEditing:YES animated:YES];
//
//
//
//}
//设置 tableView哪行可以编辑,默认是YES
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//设置tableViewCell编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;//默认是删除
// return UITableViewCellEditingStyleInsert;//插入
}
//完成编辑
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//删数据,
// NSMutableArray *onegroup = _dataArray[indexPath.section];
// [onegroup removeObjectAtIndex:indexPath.row];
////
// NSArray *aeeay = [NSArray arrayWithObjects:indexPath, nil];
// NSArray *aeeay = @[indexPath];//同下面的同等
//
//删除(可以是n行)
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSMutableArray *onegroup = _dataArray[indexPath.section];
//如果分区中只剩一行,就删整个分区
if (onegroup.count == 1) {
[_dataArray removeObject:onegroup];//删除某个分区的数据
//tableview删除section
NSIndexSet *set = [NSIndexSet indexSetWithIndex:indexPath.section];
[_tableView deleteSections:set withRowAnimation:(UITableViewRowAnimationLeft)];
}else {
//删除某行数据
[onegroup removeObjectAtIndex:indexPath.row];
//NSArray *arr = [NSArray arrayWithObjects:indexPath, nil];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationAutomatic)];
}
// [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationAutomatic)];//crash.只删除行.没有删除数据
// //三条数据显示在两行上(得先删数据)
}
//添加
if (editingStyle == UITableViewCellEditingStyleInsert) {
NSMutableArray *onegroup = _dataArray[indexPath.section];
if (onegroup.count != 0) {
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationBottom)];
//_arr1 = [[NSMutableArray alloc] initWithObjects:@"北京", nil];
[onegroup addObject:@"北京"];
_dataArray = [[NSMutableArray alloc] initWithObjects:onegroup, nil];
[_tableView reloadData];
NSLog(@"添加不上");
}
}
}
//2设置tableview能否移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//2完成移动(数据自己来处理 移动)
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//处理数据
//找获取对应的分组
NSMutableArray *onegroup = _dataArray[sourceIndexPath.section];
//先将药膳粗的元素retain(防止删除时出问题)
NSString *name = [onegroup[sourceIndexPath.row] retain];//找元素jia 1
//删除元素
[onegroup removeObject:name];
//插入到指定位置
[onegroup insertObject:name atIndex:destinationIndexPath.row];
[name release];
}
//设置cell能否移动到计划的目的地propose(代理)
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
//如果移动的范围是在一个同一个区section可以移动
if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
return proposedDestinationIndexPath;//返回目的地
}
return sourceIndexPath;//不让跨区移动
//return proposedDestinationIndexPath;//可以移动
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
//return 3;
return [_dataArray count];
}
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//3个分区 分别为 10 15 20 行
// if (section == 0) {
// return 10;
// }
// if (section == 1) {
// return 15;
// }
//return 10;
NSArray *array = _dataArray[section];
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//static NSString *identifier = @"cell";
//1从队列中取出cell对象
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//2如果没有就重新创建
if (cell == nil) {
cell= [[[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier] autorelease];
}
// //3根据section,row,对cell进行设置,显示相应的内容.
// cell.textLabel.text = [NSString stringWithFormat:@" 分区:%ld 行:%ld",indexPath.section,indexPath.row];
// //(此时的cell可能是新创建按的,可能是从队列中取来的)
// //4将设置完的cell对象返回给tabelView
NSArray *array = _dataArray[indexPath.section];
cell.textLabel.text = array[indexPath.row];
return cell;
}
////给每个分区 设置头部标题
//
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
// //分别显示 1 2 3
// return [NSString stringWithFormat:@"%ld",section+1];//索引是从0开始的
//
//
//}
//所有
//- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
//{
//
// return @[@"1",@"2",@"3"];
//
//}
////设置行高
//- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
// if (indexPath.section == 0) {
// return 80;
// }
// if (indexPath.section == 1) {
// return 150;
// }
// return 60;
//
//}
//
//检测cell被选中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@" 分区:%ld 行数:%ld",indexPath.section ,indexPath.row);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
标签:ios
原文地址:http://blog.csdn.net/zhaoguodongios/article/details/45558669