天天看點

UITableView詳解(UITableViewCell(四) 增加 删除 移動)

@implementation HMTAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    HMTRootViewController * rootRC = [[HMTRootViewController alloc]init];
    
    UINavigationController * rootNC = [[UINavigationController alloc]initWithRootViewController:rootRC];
    
    self.window.rootViewController = rootNC;
    
    
    [self.window makeKeyAndVisible];
    return YES;
}

#import <UIKit/UIKit.h>

@interface HMTRootViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>{

    int i ;
}

@property (nonatomic,retain)NSMutableArray * iPhoneArrays;
@property (nonatomic,retain)UITableView *  iPhoneTableView;

@end

#import "HMTRootViewController.h"

@interface HMTRootViewController ()

@end

@implementation HMTRootViewController

-(void)dealloc{

    RELEASE_SAFELY(_allGroupArray);
    RELEASE_SAFELY(_iPhoneTableView);
    [super dealloc];

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _iPhoneArrays = [[NSMutableArray alloc]init];
        i = 0;
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 靈活運用數組
    // 一個區的每一行資料(小數組)
    NSArray * array1 = [NSArray arrayWithObjects:@"中國",@"日本",@"南韓", nil];
    NSArray * array2 = [NSArray arrayWithObjects:@"德國",@"英國",@"法國",@"瑞士", nil];
    NSArray * array3 = [NSArray arrayWithObjects:@"美國",@"俄羅斯",@"墨西哥", nil];
    // 每一個區的資料(大數組)
    self.iPhoneArrays = [NSMutableArray arrayWithObjects:array1,array2,array3, nil];
    
    // 建立一個表單視圖
    self.iPhoneTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height - 44)
                       style:UITableViewStyleGrouped];
    
    _iPhoneTableView.separatorColor = [UIColor redColor];
    _iPhoneTableView.delegate = self;
    _iPhoneTableView.dataSource = self;
    
    [self.view addSubview:_iPhoneTableView];
    
    /*
    UIBarButtonItem * rightButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
                                    target:self action:@selector(didClickRightBarButtonItemAction:)];
    self.navigationItem.rightBarButtonItem = rightButton;
    */
    // 用系統定義好的一個edit按鈕
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.editButtonItem.title = @"編輯";
    
	// Do any additional setup after loading the view.
}

#pragma mark - 設定編輯環境,開啟/關閉

// 系統自帶按鈕
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    
    [super setEditing:editing animated:animated];
    
    #pragma mark 推薦這種寫法
    self.editButtonItem.title = [email protected]"完成":@"編輯";
    
    [_listTableView setEditing:editing animated:animated];
    NSLOG_FUNCTION;
    
}

// 自定義編輯按鈕
- (void)didClickRightBarButtonItemAction:(UIBarButtonItem *)rightButton{
    
    if (_isEditing) {
        _isEditing = NO;
        rightButton.title = @"編輯";
    } else {
        _isEditing = YES;
        rightButton.title = @"完成";
    }
    // 熟悉這種寫法
    //_isEditing = !_isEditing;
    [_listTableView setEditing:_isEditing animated:YES];
    
}

#pragma mark - 資料設定

// 設定分區
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return [_iPhoneArrays count];
    
}

// 設定每個分區的行數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [[_iPhoneArrays objectAtIndex:section] count];

}

// cell的具體實作和設定
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString * celldentifier = @"cell";

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:celldentifier];
    
    if (!cell) {
         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:celldentifier];
        i++;NSLog(@" ------%i---------",i);
    }
    
    cell.textLabel.text = [[_iPhoneArrays objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    if (section == 0) {
        return @"亞洲";
    } else if (section == 1){
        return @"歐洲";
    }else
        return @"北美洲";

}

// cell被選中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    // 取消選中後顯示的高亮狀态,也就是隻在選中的那一刻出現高亮
    [tableView deselectRowAtIndexPath:indexPath animated:YES];


}

#pragma mark - 資料編輯Edit 删除/添加

#pragma mark 詢問具體到某一區某一行能否被編輯(删除/添加)
// 詢問能否被編輯 ----- 系統預設全部cell能編輯,這個方法能夠讓你設定具體到哪一行哪一列
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

    return YES;

}

#pragma mark 傳回編輯樣式,是添加還是删除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    // 第3個分區進行增加操作
    if (indexPath.section == 2) {
        return  UITableViewCellEditingStyleInsert;
    }
    
    // 其餘分區進行删除操作
    return UITableViewCellEditingStyleDelete;
}

#pragma mark 最後編輯操作(單純的設定這個方法,cell向左滑動會出現delete操作,短信.微信.QQ都是此操作)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    // 判斷狀态
    // 删除資料
    if (editingStyle == UITableViewCellEditingStyleDelete) {
    
        // 做判斷,如果
        if ([deletaArray count] > 1) {
            
            // 先删除資料
            [[_iPhoneArrays objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];
            // 删除cell(指定行)
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section], nil] 
                                              withRowAnimation:UITableViewRowAnimationLeft];
            //[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
            
        }else{
        
            // 直接移除掉整個數組
            [_iPhoneArrays removeObjectAtIndex:indexPath.section];
            // 删除整個分區
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationLeft];
            
        }
        
    }
    
    // 添加資料
    if (editingStyle == UITableViewCellEditingStyleInsert) {
        
        NSString * text = @"天空之城";
        // 先添加資料源
        [[_iPhoneArrays objectAtIndex:indexPath.section] addObject:addCellText];
        // 自定義要插入的行數(indexPath.row + ?(?>0) ?是幾就代表添加的資料會出現在+号下面的幾個位置處 ?為0就會出現在點選加号那一行的上面)
        NSIndexPath * selfIndexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:selfIndexPath] withRowAnimation:UITableViewRowAnimationLeft];        
        
    }

}


#pragma mark - 資料編輯Edit 移動

#pragma mark 詢問具體到某一區某一行能否被編輯(移動)
// 詢問能否被移動(參數indexPath作用:指點哪些區哪些行能夠移動或者不能夠移動)
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{

    return YES;

}

#pragma mark 設定能否被移動
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{

    // 一般,移動都是發生在同區之間,資料在不同區之間移動,那麼資料一開始就應該不會編輯到一個區
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        // 允許移動(目的地)
        return proposedDestinationIndexPath;
        
    }else{
        // 不允許移動(源始地)
        return sourceIndexPath;
    
    }
    
}

#pragma mark 完成最後移動,主要操作資料就行(注:移動并不是交換,移動看看就知道了,移動到一個位置,原先所有的cell會自動向下)
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    
    // 取得要移動的資料
    NSString * moveString = [[_iPhoneArrays objectAtIndex:sourceIndexPath.section] objectAtIndex:sourceIndexPath.row];
    // 移動到目的地
    [[_iPhoneArrays objectAtIndex:sourceIndexPath.section] insertObject:moveString atIndex:destinationIndexPath.row];
    // 删除源始地的資料
    [[_iPhoneArrays objectAtIndex:sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end