ios UITableView表視圖(2)

來源:互聯網
上載者:User

標籤:des   style   io   color   ar   os   sp   for   strong   

tableView編輯

1、讓tableView處於編輯狀態

2、指定tableView哪些行可以編輯

3.指定tableView編輯的樣式(添加、刪除)

4、編輯完成(先操作資料源,再修改UI)

 tableView移動

1、讓tableView處於編輯狀態

2、指定tableView哪些行可以移動

3.移動完成

 4.監測移動過程,實現限制跨區移動

 ?論編輯還是移動,都先讓tableView進入編輯狀態。 編輯結束或者移動結束,要先修改數組或字典中的資料,在更改UI。

UITableViewController是封裝好了各種delegate和datasource,能 提高我們開發速度



<span style="font-size:18px;color:#330033;">#import "RootViewController.h"#import "RootView.h"@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>{    UITableViewCellEditingStyle _style;}@property(nonatomic,strong)RootView *myview;@property(nonatomic,strong)NSMutableArray *groupArray;@end@implementation RootViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self)    {        self.myview = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];    }    return self;}-(void)loadView{    self.view = self.myview;}- (void)viewDidLoad{    [super viewDidLoad];    self.groupArray = [NSMutableArray array];    //設定代理    self.myview.TableView.dataSource = self;    //設定代理    self.myview.TableView.delegate = self;    [self addAction];    }-(void)addAction{   //建立一個barbutton按鈕,(刪除按鈕)    UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc] initWithTitle:@"刪除" style:(UIBarButtonItemStyleDone) target:self action:@selector(daleteButton:)];    self.navigationItem.leftBarButtonItem = deleteButton;    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:(UIBarButtonItemStyleDone) target:self action:@selector(addButton:)];    self.navigationItem.rightBarButtonItem = addButton;    //建立一個假資料    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.groupArray addObject:tempArray];    }//    NSLog(@"%@",self.groupArray[0]);        }//編輯狀態//刪除按鈕-(void)daleteButton:(UIBarButtonItem *)sender{    //當點擊"刪除"按鈕的時候,觸發事件,給他一個編輯狀態(刪除狀態)    _style = UITableViewCellEditingStyleDelete;    //一個BOOL 接收此時的編輯狀態    BOOL flag = self.myview.TableView.editing;    //設定編輯狀態    [self.myview.TableView setEditing:!flag animated:YES];    }//添加按鈕-(void)addButton:(UIBarButtonItem *)sender{    _style = UITableViewCellEditingStyleInsert;    BOOL flag = self.myview.TableView.editing;    [self.myview.TableView setEditing:!flag animated:YES];    }/*-----------------------UITableViewDataSource-----------------------------*///下面的方法都是遵循UITableViewDataSource協議的,所以先在viewDidLoad先去設定代理//設定幾個分區- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return self.groupArray.count;}//設定一個分區裡面有幾行- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [self.groupArray[section] count];}//設定cell- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    //標識    static NSString *cell_id = @"cell_id";    //建立cell    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id];    //判斷在重用池中有沒有cell,如果沒有就建立    if (nil == cell)    {        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:cell_id];    }    //給cell設定標題    cell.textLabel.text = self.groupArray[indexPath.section][indexPath.row];       //給cell設定副標題    cell.detailTextLabel.text = @"我中獎了";        return cell;}//指定哪些行可以被編輯-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{    //例如讓單行處於被編輯狀態    if(indexPath.row % 2)    {        return NO;    }    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.groupArray[indexPath.section]removeObjectAtIndex:indexPath.row];    //重新整理資料    [tableView reloadData];    //重新整理資料動畫//    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];    }    else if (editingStyle == UITableViewCellEditingStyleInsert)    {        NSString *string = @"測試資料";        //修改資料來源(實現添加)        [self.groupArray[indexPath.section]insertObject:string atIndex:indexPath.row+1];                //修改UI        //        [tableView reloadData];        //        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationTop)];        //添加資料到顯示到添加行下面        NSIndexPath *indes = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];        //重新整理資料帶動畫效果        [tableView insertRowsAtIndexPaths:@[indes] withRowAnimation:(UITableViewRowAnimationTop)];            }    }/*-------------------------------tableView移動----------------------------------------*///指定哪些被移動-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{        return YES;}//移動完成-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{        //記錄    id obj = self.groupArray[sourceIndexPath.section][sourceIndexPath.row];        //移除    [self.groupArray[sourceIndexPath.section]removeObjectAtIndex:sourceIndexPath.row];    //添加    [self.groupArray[destinationIndexPath.section]insertObject:obj atIndex:destinationIndexPath.row];    //更新資料來源    [tableView reloadData];        }//限制移動地區-(NSIndexPath*)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{    //先判斷源和目的的分組是否相同,    if (sourceIndexPath.section != proposedDestinationIndexPath.section)    {        //如果不同,返回源的起始位置        return sourceIndexPath;    }    else    {        //如果相同,返回目的的位置        return proposedDestinationIndexPath;    }}</span>

ios UITableView表視圖(2)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.