iOS-表格式資料的添加 刪除

來源:互聯網
上載者:User

標籤:

////  ViewController.m//  表格的修改////  Created by YaguangZhu on 15/8/16.//  Copyright (c) 2015年 YaguangZhu. All rights reserved.//#import "ViewController.h"@interface ViewController ()<UITabBarControllerDelegate>@property (nonatomic,strong)NSMutableArray *dataList;@property (nonatomic,strong)UITableView *tableView;@end@implementation ViewController- (UITableView *)tableView{    if (_tableView == nil) {        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];                _tableView.dataSource = self;        _tableView.delegate = self;                [self.view addSubview:_tableView];    }    return _tableView;}- (NSMutableArray *)dataList{    if (_dataList == nil) {        _dataList = [NSMutableArray arrayWithObjects:@"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwuwangwuwangwuwangwuwangwu", nil];    }    return _dataList;}- (void)viewDidLoad {    [super viewDidLoad];        [self tableView];        // 開始編輯,一旦editing == YES就預設開啟刪除模式    self.tableView.editing = YES;    // Do any additional setup after loading the view, typically from a nib.}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return self.dataList.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *ID = @"Cell";        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];        if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];    }        // 設定表格    cell.textLabel.text = self.dataList[indexPath.row];        return cell;}// 只要實現了此方法,就能夠支援手勢拖拽刪除了,刪除需要自己幹!/** UITableViewCellEditingStyleNone, UITableViewCellEditingStyleDelete,     刪除 UITableViewCellEditingStyleInsert      添加 */- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    if (editingStyle == UITableViewCellEditingStyleDelete) {        NSLog(@"要刪除");                // MVC => 資料是儲存在模型中        // 1. 刪除self.dataList中indexPath對應的資料        [self.dataList removeObjectAtIndex:indexPath.row];        NSLog(@"%@", self.dataList);                // 2. 重新整理表格(重新載入資料)        // 重新載入所有資料        //        [self.tableView reloadData];        // deleteRowsAtIndexPaths讓表格控制項動畫刪除指定的行        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];    } else if (editingStyle == UITableViewCellEditingStyleInsert) {        NSLog(@"要添加資料");                // 1. 向數組添加資料        [self.dataList insertObject:@"王小二" atIndex:indexPath.row + 1];        // 2. 重新整理表格        //        [self.tableView reloadData];        // insertRowsAtIndexPaths讓表格控制項動畫在指定indexPath添加指定行        // 建立一個indexPath        NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];                [self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];    }}#pragma mark - 代理方法// 返回編輯樣式,如果沒有實現此方法,預設都是刪除- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{    //    if (indexPath.row % 2) {    //        return UITableViewCellEditingStyleInsert;    //    } else {    //        return UITableViewCellEditingStyleDelete;    //    }    return UITableViewCellEditingStyleInsert;}// 只要實現此方法,就可以顯示拖動控制項- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{    // 介面資料UITableView已經完成了    // 調整資料即可    //    [self.dataList exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];    // 1. 將源從數組中取出    id source = self.dataList[sourceIndexPath.row];    // 2. 將源從數組中刪除    [self.dataList removeObjectAtIndex:sourceIndexPath.row];    NSLog(@"%@", self.dataList);        // 3. 將源插入到數組中的目標位置    [self.dataList insertObject:source atIndex:destinationIndexPath.row];        NSLog(@"%@", self.dataList);}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

 

iOS-表格式資料的添加 刪除

聯繫我們

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