猫猫学IOS(十四)UI之UITableView扩充_表格的修改_(增删移动)

来源:互联网 发布:mac推出磁盘快捷键 编辑:程序博客网 时间:2024/06/08 14:09

猫猫分享,必须精品
素材代码地址:http://blog.csdn.net/u013357243/article/details/44727823
原文地址:http://blog.csdn.net/u013357243?viewmode=contents

先看效果图

这里写图片描述 这里写图片描述 这里写图片描述 这里写图片描述

代码

//ps:新建iOS交流学习群:304570962 可以加猫猫QQ:1764541256 或则微信znycat 让我们一起努力学习吧。 原文:http://blog.csdn.net/u013357243?viewmode=contents//  HMViewController.m//  06-表格的修改////  Created by apple on 14-8-19.//  Copyright (c) 2014年 itcast. All rights reserved.//#import "HMViewController.h"@interface HMViewController () <UITableViewDataSource, UITableViewDelegate>/** 数据列表 */@property (nonatomic, strong) NSMutableArray *dataList;@property (nonatomic, strong) UITableView *tableView;@end@implementation HMViewController- (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:@"猫猫1号", @"猫猫1号", @"猫猫2号", @"猫猫3号", @"猫猫4号", @"猫猫5号",@"猫猫6号", @"猫猫7号", @"猫猫8号",@"猫猫9号", @"猫猫1号", @"猫猫1号",@"猫猫1号", @"猫猫1号", @"猫猫1号",@"猫猫1号", @"猫猫1号", @"猫猫1号",@"猫猫1号", @"猫猫1号", @"猫猫1号",@"猫猫1号", @"猫猫1号", @"猫猫1号",@"猫猫1号", @"猫猫1号", @"猫猫1号",@"猫猫1号", @"猫猫1号", @"猫猫1号",@"猫猫1号", @"猫猫1号", @"猫猫1号",nil];    }    return _dataList;}- (void)viewDidLoad{    [super viewDidLoad];    [self tableView];    // 开始编辑,一旦editing == YES就默认开启删除模式    self.tableView.editing = YES;}- (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];    }}// 只要实现此方法,就可以显示拖动控件- (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);}#pragma mark - 代理方法// 返回编辑样式,如果没有实现此方法,默认都是删除- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{//    if (indexPath.row % 2) {//        return UITableViewCellEditingStyleInsert;//    } else {//        return UITableViewCellEditingStyleDelete;//    }    return UITableViewCellEditingStyleInsert;}@end

UITableView支持删除手势

只要实现了此方法,就能够支持手势拖拽删除了,删除需要自己干!

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

UITableViewCellEditingStyleNone, 没效果
UITableViewCellEditingStyleDelete, 删除
UITableViewCellEditingStyleInsert 添加

删除中要做的:
重新加载数据时候用[self.tableView reloadData];会效率低下

// MVC => 数据是保存在模型中        // 1. 删除self.dataList中indexPath对应的数据        [self.dataList removeObjectAtIndex:indexPath.row];        NSLog(@"%@", self.dataList);        // 2. 刷新表格(重新加载数据)        // 重新加载所有数据//        [self.tableView reloadData];        // deleteRowsAtIndexPaths让表格控件动画删除指定的行        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];

UITableView 增加

要实现守代理方法 tableView.delegate = self;

// 返回编辑样式,如果没有实现此方法,默认都是删除- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{//    if (indexPath.row % 2) {//        return UITableViewCellEditingStyleInsert;//    } else {//        return UITableViewCellEditingStyleDelete;//    }    return UITableViewCellEditingStyleInsert;}

下面是在- (void)tableView:(UITableView )tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath )indexPath 方法中设置的
判断编辑样式,是删除还是增添,然后做相应操作

 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];

UITableView 移动

// 只要实现此方法,就可以显示拖动控件- (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);}

ps:新建iOS交流学习群:304570962 可以加猫猫QQ:1764541256 或则微信znycat 让我们一起努力学习吧。 原文:http://blog.csdn.net/u013357243?viewmode=contents

11 0
原创粉丝点击