iOS-表格数据的添加 删除

//
//  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
时间: 2024-08-03 02:33:14

iOS-表格数据的添加 删除的相关文章

iOS文件的管理(添加,删除,拷贝,移动)

#import "ViewController.h" @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self handleNSFileManage]; } // 文件管理 - (void)handleNSFileManage{ // NSFileManager 是一个单例类,我们称之为文件管理类,是一个专门用来管理文件的工具,主要可以完成以下功能:文件的添加,文件的删除,文件的移动,

EF6基础系列(12)--- EF进行批量添加/删除

EF6添加了批量添加/删除实体集合的方法,我们可以使用DbSet.AddRange()方法将实体集合添加到上下文,同时实体集合中的每一个实体的状态都标记为Added,在执行SaveChange()方法时为每个实体执行Insert操作:同样的我们使用DbSet.RemoveRange()方法将集合中的所有实体都标记为deleted状态,在执行SaveChange()方法时为每一条数据执行delete操作. 通过AddRange()和RemoveRange()方法可以有效提升性能,所以建议在进行不批

【iOS开发-76】Private Contacts案例:导航控制器使用、数据传递、第三方类库使用、tableViewCell的添加删除、数据存储等

(1)效果 (2)源代码与第三方类库下载 http://download.csdn.net/detail/wsb200514/8155979 (3)总结 --导航控制器,可以直接用代码的push和pop来控制控制器之间的跳转.也可以使用storyboard的segue来:这里又涉及2种,一种是直接用按钮拖拽到另一个控制器形成segue,这种segue不可拦截,如果点击直接跳转.另一种是从一个控制器拖拽到另一个控制器形成的segue,这种segue没有明确的点击谁来跳转,所以有一个performS

编辑 Ext 表格(一)——— 动态添加删除行列

一.动态增删行 在 ext 表格中,动态添加行主要和表格绑定的 store 有关, 通过对 store 数据集进行添加或删除,就能实现表格行的动态添加删除. (1) 动态添加表格的行  gridStore.add({}); (2) 动态删除表格的行 gridStore.removeAt(gridStore.count() - 1); 二.动态增删列 在 ext 表格中,动态添加列主要通过修改表格绑定的 column 元素, 通过对 column 元素集进行添加或删除,然后重新渲染表格,就能实现表

Sql—表格的建立,删除,数据的建立与删除-总结篇

一,Sql—表格的建立,删除,数据的建立与删除 Sql表格的建立公式 If exists (select * from sysobjects where <表名> Drop table <表名> Create table <表名> (<列名1> <数据类型> <约束类型> <是否为空>, <列名2> <数据类型> <约束类型> <是否为空>,    (约束类型如果没有可以不写

向后端请求数据 以及像后端发送数据要求(删除数据和添加数据)

删除数据和添加数据只能后端操作 删除数据和添加数据都要用到的html(一)部分 <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="../css/bootstrap.min.css"

UITableView数据的添加、删除、移动

数据的添加.删除4个步骤: 1.现在导航视图上面添加一个按钮,可以是系统自带的编辑按钮editButtonItem 2.实现方法让将要执行删除添加操作的表视图处于编辑状态 3.指定表视图中哪些行可以处于编辑状态,默认所有行都可以进行编辑 4.指定编辑样式,到底是删除还是添加,此方法如果不重写的话,默认是删除样式 5.不管是删除还是添加,这个方法才是操作的核心方法,当点击删除/添加按钮时,需要做什么事情,怎么样才能完成删除或者添加操作 步骤:5.1表视图开始更新 5.2根据不同的编辑样式进行写代码

jquery动态添加删除一行数据

<html> <head> <title>添加.删除一行</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <script type="text/javascript" src="../js/jquery-1.9.1.js"></script>

二.jquery.datatables.js表格数据添加

1.后台php public function addtable(){ $data = $_POST; if(M('yanfa_project')->add($data)){ $this->ajaxReturn("success"); } } 二.jquery.datatables.js表格数据添加