ios -- UITableView簡單的移動(dòng)刪除Demo

Untitled.gif

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

#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];
    
    //初始化三個(gè)人名
    
    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的右邊有一個(gè)小箭頭,距離右邊有十幾像素;
    
    //    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//cell右邊有一個(gè)藍(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)這個(gè)方法拄氯,就實(shí)現(xiàn)了默認(rèn)滑動(dòng)刪除!K场R氚亍!姐霍!
    
    if (editingStyle != UITableViewCellEditingStyleDelete)
        
        return;
    
    //刪除數(shù)據(jù)模型
    
    [marr removeObjectAtIndex:indexPath.row];
    
    [_table deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    
}

#pragma mark - 移動(dòng)操作

- (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)系作者
  • 序言:七十年代末鄙麦,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子镊折,更是在濱河造成了極大的恐慌胯府,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件恨胚,死亡現(xiàn)場離奇詭異骂因,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)赃泡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進(jìn)店門寒波,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人升熊,你說我怎么就攤上這事俄烁。” “怎么了级野?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵页屠,是天一觀的道長。 經(jīng)常有香客問我蓖柔,道長辰企,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任渊抽,我火速辦了婚禮蟆豫,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘懒闷。我一直安慰自己十减,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布愤估。 她就那樣靜靜地躺著帮辟,像睡著了一般。 火紅的嫁衣襯著肌膚如雪玩焰。 梳的紋絲不亂的頭發(fā)上由驹,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼蔓榄。 笑死并炮,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的甥郑。 我是一名探鬼主播逃魄,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼澜搅!你這毒婦竟也來了伍俘?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤勉躺,失蹤者是張志新(化名)和其女友劉穎癌瘾,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體饵溅,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡妨退,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了蜕企。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片碧注。...
    茶點(diǎn)故事閱讀 40,675評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖糖赔,靈堂內(nèi)的尸體忽然破棺而出萍丐,到底是詐尸還是另有隱情,我是刑警寧澤放典,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布逝变,位于F島的核電站,受9級特大地震影響奋构,放射性物質(zhì)發(fā)生泄漏壳影。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一弥臼、第九天 我趴在偏房一處隱蔽的房頂上張望宴咧。 院中可真熱鬧,春花似錦径缅、人聲如沸掺栅。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽氧卧。三九已至,卻和暖如春氏堤,著一層夾襖步出監(jiān)牢的瞬間沙绝,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留闪檬,地道東北人星著。 一個(gè)月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像粗悯,于是被迫代替她去往敵國和親强饮。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評論 2 360

推薦閱讀更多精彩內(nèi)容