TableViewController的添加,刪除,移動

來源:互聯網
上載者:User

標籤:des   style   io   ar   os   sp   for   strong   資料   

#import "RootTableViewController.h"

 

@interface RootTableViewController ()

{

    UITableViewCellEditingStyle _style;

}

@property(nonatomic,strong)NSMutableArray *array;

@end

 

@implementation RootTableViewController

 

- (id)initWithStyle:(UITableViewStyle)style

{

    self = [super initWithStyle:style];

    if (self) {

        // Custom initialization

        self.array=[NSMutableArray array];

        

           }

    return self;

}

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    

    //在數組裡存一些資料

    for (int i=0; i<3; i++) {

        NSMutableArray *tempArray=[NSMutableArray array];

        for (int j=0; j<5; j++) {

            [tempArray addObject:[NSString stringWithFormat:@"第%d組,第%d個人",i,j]];

        }

        [self.array addObject:tempArray];

    }

   // 設定代理

    self.tableView.dataSource=self;

    self.tableView.delegate=self;

 

    //在導覽列兩側添加兩個barbutton

    UIBarButtonItem *bar=[[UIBarButtonItem alloc] initWithTitle:@"編輯" style:(UIBarButtonItemStyleDone) target:self action:@selector(barAction:)];

    self.navigationItem.leftBarButtonItem=bar;

    

    UIBarButtonItem *bar1=[[UIBarButtonItem alloc] initWithTitle:@"添加" style:(UIBarButtonItemStyleDone) target:self action:@selector(barAction1:)];

    self.navigationItem.rightBarButtonItem=bar1;

 

    

    

}

//barbutton編輯事件

-(void)barAction:(UIBarButtonItem *)sender

{

    _style=UITableViewCellEditingStyleDelete;

    BOOL flag=self.tableView.editing;

    [self.tableView setEditing:!flag animated:YES];

}

//barbutton添加事件

-(void)barAction1:(UIBarButtonItem *)sender

{

    _style=UITableViewCellEditingStyleInsert;

    BOOL flag=self.tableView.editing;

    [self.tableView setEditing:!flag animated:YES];

}

//設定哪些行能編輯(預設全部都能)

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

    return  YES;

}

//確定編輯狀態(刪除|添加)

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return _style;

}

//提交編輯

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    if(editingStyle==UITableViewCellEditingStyleDelete)

    {

        //刪除

        [self.array[indexPath.section]removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];

    }

    else

    {

        //添加

        NSString *s=@"測試資料";

        [self.array[indexPath.section]insertObject:s atIndex:indexPath.row+1];

        NSIndexPath *index=[NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];

        [tableView insertRowsAtIndexPaths:@[index] withRowAnimation:(UITableViewRowAnimationRight)];

        

    }

    

}

//哪些行能移動

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

    return YES;

}

 

//完成移動

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

{

    //先賦值

    id obj=self.array[sourceIndexPath.section][sourceIndexPath.row];

    //再刪除

    [self.array[sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];

    //最後添加

    [self.array[destinationIndexPath.section]insertObject:obj atIndex:destinationIndexPath.section];

}

//檢查越界

-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath

{

    

    if(sourceIndexPath.section!=proposedDestinationIndexPath.section)

    {

        return sourceIndexPath;

    }

    else

    {

        return proposedDestinationIndexPath;

    }

}

 

TableViewController的添加,刪除,移動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.