tableView對(duì)cell的編輯

表的一種添加 cell 方法
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *_tableView;
//存放標(biāo)的數(shù)據(jù)
NSMutableArray *_dataArray;
}
@end

@implementation ViewController
-(void)dealloc
{
    [_dataArray release];
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, 320, 480-64) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource =self;
    [self.view addSubview:_tableView];
    //初始化數(shù)組
    _dataArray = [[NSMutableArray alloc] initWithObjects:@"a",@"b",@"c",@"d",@"e", nil];
    //添加導(dǎo)航右邊的按鈕
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemAdd) target:self action:@selector(addData)];
    self.navigationItem.rightBarButtonItem = rightBtn;
    [rightBtn release];
    /*
     表的添加:  非編輯狀態(tài)下的添加
     1.表的添加一般不用reloadData
     2.表有自己的添加方法
     insertRowsAtIndexPaths:
     withRowAnimation:
     3.通過行號(hào)和區(qū)號(hào)找打?qū)?yīng)的索引
     NSIndexPath indexPathForRow:
     inSection
     */
}
-(void)addData
{
    //1.讓數(shù)組的元素增加
    [_dataArray addObject:@"新數(shù)據(jù)"];
    //2.刷新表
    //[_tableView reloadData];
    
    //獲取區(qū)數(shù)
    int section = 0;
    //獲取行數(shù),行號(hào) = 數(shù)組元素個(gè)數(shù) - 1
    int row = _dataArray.count-1;

    //根據(jù)行數(shù)和區(qū)數(shù)找到對(duì)應(yīng)的索引
    //indexPathForRow 要增加的行號(hào)  inSection 在哪個(gè)區(qū)
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];

    //insertRowsAtIndexPaths 表的插入數(shù)據(jù)方法.在指定行插入數(shù)據(jù)  insert:插入
    //參數(shù)1:數(shù)組里面的是 索引indexpath
    [_tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationRight)];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cellID"];
    }
    //設(shè)置cell文本內(nèi)容
    cell.textLabel.text = [_dataArray objectAtIndex:indexPath.row];
    return cell;
}
@end

表對(duì)cell的編輯方法,包括添加新的cell、刪除cell,以及對(duì)cell的移動(dòng)

#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tabelView;
    NSMutableArray *_dataArray;
}
@end

@implementation ViewController
-(void)dealloc
{
    [_tabelView release];
    [_dataArray release];
    [super dealloc];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    _tabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, 320, 480-64) style:(UITableViewStylePlain)];
    _tabelView.delegate = self;
    _tabelView.dataSource = self;
    [self.view addSubview:_tabelView];
    _dataArray = [[NSMutableArray alloc] initWithObjects:@"a",@"b",@"c",@"d",@"e"@"f",@"g", nil];
    //導(dǎo)航條右邊按鈕
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemEdit) target:self action:@selector(editingData)];
    self.navigationItem.rightBarButtonItem = rightBtn;
    [rightBtn release];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cellID"];
    }
    cell.textLabel.text = [_dataArray objectAtIndex:indexPath.row];
    return cell;
}
#pragma mark 表的編輯
-(void)editingData
{
    //editing 表的編輯
    //改變標(biāo)的編輯狀態(tài)
    _tabelView.editing = !_tabelView.editing;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //UITableViewCellEditingStyle:cell的編輯樣式
    
    //UITableViewCellEditingStyleNone 當(dāng)cell移動(dòng)的時(shí)候使用
    //默認(rèn)是UITableViewCellEditingStyleDelete 刪除樣式
    //UITableViewCellEditingStyleInsert 插入樣式
    return UITableViewCellEditingStyleInsert;
}
#pragma mark -- 提交編輯樣式的時(shí)候調(diào)用 表的插入數(shù)據(jù),刪除數(shù)據(jù)都是使用這個(gè)方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//插入新數(shù)據(jù)
    //1.在指定行插入數(shù)據(jù) atIndex 在指定的位置insertObject插入數(shù)據(jù)
    [_dataArray insertObject:@"新數(shù)據(jù)" atIndex:indexPath.row];
    //2.刷新表,用表的插入方法
    [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];
    
//刪除指定的行
//[_dataArray removeObjectAtIndex:indexPath.row];
////刷新表,用表的刪除方法
//[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];
}

#pragma mark -- 刪除狀態(tài)下使用  返回刪除按鈕的標(biāo)題 默認(rèn)delete
//當(dāng)cell的編輯樣式為UITableViewCellEditingStyleDelete才有效
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"刪除";
}

#pragma mark -- 表的移動(dòng)方法,當(dāng)移動(dòng)單元格的時(shí)候使用
//source資源destination目的地
//sourceIndexPath 要引動(dòng)的數(shù)據(jù)的索引
//destinationIndexPath要移動(dòng)后的索引,目的地的索引
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    //1.取出數(shù)組中的元素(原來的位置)
    NSString *string = [_dataArray objectAtIndex:sourceIndexPath.row];
    //2.從數(shù)組中移除要移動(dòng)的元素
    [_dataArray removeObject:string];
    //3.把要移動(dòng)的元素添加到目的地的位置(在目的地插入數(shù)據(jù))
    [_dataArray insertObject:string atIndex:destinationIndexPath.row];
}
@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,614評(píng)論 4 61
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,688評(píng)論 19 139
  • 我特別害怕離別,害怕畢業(yè),這種感覺真的令人呼吸都困難,可是,不管你有多么的不舍,那一天總會(huì)來臨,不管是以哪種方...
    太烊閱讀 242評(píng)論 0 0
  • 丈夫和女兒 我該怎么選?講述者 微瀾(化名) 長春晚報(bào)記者 劉冰 與丈夫常年異地分居,她獨(dú)自帶著女兒與父母同住。眼...
    花香四溢rene閱讀 1,208評(píng)論 0 3
  • 從生下來一直到工作,基本上是一帆風(fēng)順,沒有什么大悲大喜,生在亦長在農(nóng)村,總感覺生存空間和周圍的大自然一樣,清新,透...
    maia_1718閱讀 322評(píng)論 0 0

友情鏈接更多精彩內(nèi)容