ios -- UITableView簡單的移動刪除Demo

Untitled.gif

當(dāng)我們點(diǎn)擊添加按鈕時(shí)可以向提示框輸入數(shù)據(jù)添加到表格,當(dāng)我們點(diǎn)擊編輯時(shí)我們可以對表格進(jìn)行移動刪除操作

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    UITableView *_table;
    
    NSMutableArray *marr;
    
    UIButton *leftBtn;
    
}

@end

@implementation ViewController
//這里主要實(shí)現(xiàn)表格的初始化,數(shù)組的初始化,左右導(dǎo)航按鈕的初始化

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    // Do any additional setup after loading the view, typically from a nib.
    
    self.title = @"表格";
    
    //添加編輯按鈕
    
    leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [leftBtn setTitle:@"編輯" forState:UIControlStateNormal];
    
    [leftBtn setTitle:@"完成" forState:UIControlStateSelected];
    
    leftBtn.frame = CGRectMake(0, 0, 80, 40);
    
    leftBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    
    //標(biāo)題顏色
    
    [leftBtn setTitleColor:[UIColor blueColor]forState:UIControlStateNormal];
    
    [leftBtn addTarget:self action:@selector(leftBarBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:leftBtn];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarBtnClicked:)];
    
    //創(chuàng)建表格并初始化
    
    _table = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    _table.delegate = self;
    
    _table.dataSource = self;
    
    //將表格添加到視圖
    
    [self.view addSubview:_table];
    
    //初始化三個人名
    
    marr = [NSMutableArray arrayWithObjects:@"王二",@"劉山",@"李四", nil];
    
}
#pragma mark - 導(dǎo)航按鈕點(diǎn)擊觸發(fā)方法

- (void)leftBarBtnClicked:(id)sender {
    
    //設(shè)置tableview編輯狀態(tài)
    
    BOOL flag = !_table.editing;
    
    [_table setEditing:flag animated:YES];
    
    leftBtn.selected = flag;
    
}




- (void)rightBarBtnClicked:(id)sender {
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"請輸入"
                              
                                                        message:@"請輸入姓名"
                              
                                                       delegate:self cancelButtonTitle:@"確定"
                              
                                              otherButtonTitles:@"取消", nil];
    
    [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
    
    [alertView show];
    
}




#pragma mark  獲得輸入框里的值

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
    
    if ([buttonTitle isEqualToString:@"確定"]){
        
        UITextField *tf=[alertView textFieldAtIndex:0];//獲得輸入框
        
        NSString * res = tf.text;//獲得值
        
        NSLog(@"%@",res);
        
        [marr addObject:res];
        
        [_table reloadData];
        
    }
    
}
#pragma mark - 表格方法

#pragma mark 選中行

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    // 取消選中狀態(tài)
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}


//表格有多少行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return marr.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];
    
    if (!cell) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@""];
        
    }
    
    //將人名添加到表格
    
    cell.textLabel.text = marr[indexPath.row];
    
    //    cell.accessoryType = UITableViewCellAccessoryNone;//cell沒有任何的樣式
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//cell的右邊有一個小箭頭,距離右邊有十幾像素;
    
    //    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//cell右邊有一個藍(lán)色的圓形button;
    
    //    cell.accessoryType = UITableViewCellAccessoryCheckmark;//cell右邊的形狀是對號;
    // 設(shè)置右側(cè)視圖為自定義視圖
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem];
    myButton.frame = CGRectMake(0, 0, 50, 30);
    [myButton setTitle:@"購買" forState:UIControlStateNormal];
    [myButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];

    cell.accessoryView = myButton;
    return cell;
    
}
#pragma mark - 提交編輯操作

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return YES;
    
}

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

{
    
    //只要實(shí)現(xiàn)這個方法,就實(shí)現(xiàn)了默認(rèn)滑動刪除?。。。?!
    
    if (editingStyle != UITableViewCellEditingStyleDelete)
        
        return;
    
    //刪除數(shù)據(jù)模型
    
    [marr removeObjectAtIndex:indexPath.row];
    
    [_table deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    
}

#pragma mark - 移動操作

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // Return NO if you do not want the item to be re-orderable.
    
    return YES;
    
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    
    //起始位置
    
    NSInteger fromRow = fromIndexPath.row;
    
    //終止位置
    
    NSInteger toRow = toIndexPath.row;
    
    NSLog(@"%ld,%ld",fromRow,toRow);
    
    //先取出起始位置的數(shù)據(jù)
    
    NSString *fromContent = marr[fromRow];
    
    //把起始位置的數(shù)據(jù)插入終止位置
    
    [marr insertObject:fromContent atIndex:toRow];
    
    NSLog(@"%@",marr);
    
}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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