继续上篇UITableView和UITableViewController

打开BNRItemsViewController.m,在类扩展中添加如下属性:

@property (nonatomic, strong) IBOutlet UIView *headerView;

  在XIB文件中,headerView是最顶层的对象。该视图包含的对象要使用weak引用。

并在implementation部分增加如下方法:

 - (IBAction)addNewItem:(id)sender {

 }

 - (IBAction)toggleEditingMode:(id)sender {

 }

  File -> New ->File... -> iOS ->User Interface -> Empty,命名为:HeaderView。

选中刚创建的HeaderView.xib,选中File's Owner,Identity Inspector -> Custom Class 中的class改为BNRItemsViewController。

在canvas中拖入UIView视图,将其Attributes Inspector的Simulated Metrics设置为如下:

为将该UIView对象完全透明的,选择Attributes Inspector -> View -> Background -> Clear Color。

  调整UIView的大小,再往该视图中添加两个按钮,如下所示:

  按住Control键,点击File's Owner,拖到UIView中,与headerView实例变量建立连接。将Edit按钮与toggleEditingMode:建立连接,将New与addNewItem:建立连接。

  我们需要手动添加如下代码,让BNRItemsViewController加载XIB文件:

 - (UIView *)headerView {
// 如果还没加载headerView
if (!_headerView) {
// 加载HeaderView.xib
[[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil];
}
return _headerView;
}

  通过发送loadNibNamed:owner:options:方法给应用程序的bundle,任何对象都能加载一个XIB文件。

  现在需要告诉表视图关于头视图的信息,修改viewDidLoad方法如下:

 - (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"]; UIView *header = self.headerView;
[self.tableView setTableHeaderView:header];
}

  运行程序,结果为:

  修改toggleEditingMode:方法如下:

 - (IBAction)toggleEditingMode:(id)sender {
// 现在是否处于编辑模式
if (self.isEditing) {
// 改变按钮的文本为Edit,并关闭编辑模式
[sender setTitle:@"Edit" forState:UIControlStateNormal];
[self setEditing:NO animated:YES];
} else {
// 改变按钮的文本为Done,并开启编辑模式
[sender setTitle:@"Done" forState:UIControlStateNormal];
[self setEditing:YES animated:YES];
}
}

  运行程序,结果为:

  为了实现点击New按钮可添加新项目,修改addNewItem:方法如下:

 - (IBAction)addNewItem:(id)sender {
// 创建一个新的BNRItem,并将其添加到sharedStore中
BNRItem *newItem = [[BNRItemStore sharedStore] createItem];
// 获取newItem在数组中的位置
NSInteger lastRow = [[[BNRItemStore sharedStore] allItems] indexOfObject:newItem];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:lastRow inSection:];
// 将newItem插入表中
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}

  UITableView的dataSource决定表视图显示的行数,要确保UITableView和dataSource的行数要一致。

  当删除一个cell的时候,1)要从UITableView中删除该行;2)将BNRItem从BNRItemStore中删除。

在BNRItemStore.h中,声明新方法如下:

- (void)removeItem:(BNRItem *)item;

在BNRItemStore.m中,实现removeItem:方法如下:

- (void)removeItem:(BNRItem *)item {
[self.privateItems removeObjectIdenticalTo:item];
}

删除行时,tableView:commitEditingStyle:forRowAtIndexPath:将被发送给dataSource,实现该方法:

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
// 如果表视图要求进行删除命令
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSArray *items = [[BNRItemStore sharedStore] allItems];
BNRItem *item = items[indexPath.row];
[[BNRItemStore sharedStore] removeItem:item];
// 将该行从表视图中删除,并附带动画
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}

移动行:

在BNRitem.h中添加一个方法,用来改变allItems中items的顺序,方法如下:

- (void)moveItemAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex;

在BNRitem.m的实现代码为:

- (void)moveItemAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex {
if (fromIndex == toIndex) {
return;
}
BNRItem *item = self.privateItems[fromIndex];
[self.privateItems removeObjectAtIndex:fromIndex];
[self.privateItems insertObject:item atIndex:toIndex];
}

tableView:moveRowAtIndexPath:toIndexPath:用于改变一个UITableView中的行。

在BNRItemsViewController.m中添加如下代码:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
[[BNRItemStore sharedStore] moveItemAtIndex:sourceIndexPath.row toIndex:destinationIndexPath.row];
}

运行结果如下:

程序代码:https://github.com/wjq332608421/learngit/blob/master/Homepwner.zip

UITableView的编辑操作的更多相关文章

  1. UITableView的编辑模式

    UITableView可以分普通模式和Editing模式两种,这里我们着重讨论Editing模式,Editing模式中又分三种操作:Insert.Delete. Reallocted.Insert和D ...

  2. UI基础:UITableView的编辑和移动

    相对UITableViiew进行编辑,必须设置代理,让代理遵循UITableViewDataSource和UITableViewDelegate协议.因为需要代理实现协议中几个必须的方法. UITab ...

  3. iOS之UITableView带滑动操作菜单的Cell

    制作一个可以滑动操作的 Table View Cell 本文翻译自 http://www.raywenderlich.com/62435/make-swipeable-table-view-cell- ...

  4. Html简单demo_html列表中进行编辑操作

    html列表中进行编辑操作 <div class="_sort_box" style="float: left;"><?php echo $v ...

  5. linux --> VIM的列编辑操作

    VIM的列编辑操作   一.删除列 1.光标定位到要操作的地方. 2.CTRL+v 进入“可视 块”模式,选取这一列操作多少行. 3.d 删除.   二.插入列 插入操作的话知识稍有区别.例如在每一行 ...

  6. Asp.Net MVC +EntityFramework主从表新增编辑操作的实现(删除操作怎么实现?)

    Asp.Net MVC +EntityFramework主从表新增编辑操作的实现 对于MVC中同时对主从表的表单操作在网上现有的解决很少,而这样的操作在做业务系统中是经常为遇到的.我在网上搜索了很久都 ...

  7. 【vim】插入模式与常用编辑操作

    vim不像很多编辑器那样一启动便可以直接编辑文本,需要在普通模式按下i, a等键才会进入插入模式进行文本编辑. 如何进入插入模式 以下的命令都会让vim从普通模式切换到插入模式,但命令执行后的字符插入 ...

  8. 我也说说Emacs吧(5) - 基本编辑操作

    基本编辑操作 进入编辑模式 标准的emacs用户是遇不到这一节的,因为默认就可以编辑.但是spacemacs用户需要先学习一下强大的vi的模式切换功能了. vi的一个重要特点就是命令特别多,所以一旦学 ...

  9. 让UITableView进入编辑模式

    1.UITableView对象有一个editing属性,设为YES时,该对象会进入编辑模式(editing mode).表格视图进入编辑模式后,用户可以管理表格中得行,如改变行的排列顺序.增加行或删除 ...

随机推荐

  1. wap2app(九)-- 使用mui.previewImage之后,页面a链接不能跳转

    使用Hbuilder的长按保存图片的预览图片之后,页面所有的a链接都不能跳转. 解决办法: 可以使用下面绑定tap利用js跳转,亲测有效. mui('body').on( 'tap' , 'a' , ...

  2. 章节三、5-Getters-Setters和this关键字part01

    我们新建第一个Car类: package introduction; public class Car { //颜色 private String color; //生成商 private Strin ...

  3. (办公)Mysql入门

    数据库的操作:1.用 SHOW 显示已有的数据库show databases 2.创建数据库:create database 创建数据库create database db_name3.删除数据库:d ...

  4. SQL Server Alert发送告警邮件少了的原因

    最近突然发现我们部署在数据库上面的告警(Alert),当错误日志里面出现错误时,并不是每个错误日志都会发送邮件出来.如下所示,设置了告警"SQL Server Severity Event ...

  5. fedora 28/29 配置 C++ 环境

    最近 使用C++ 开发 更换机器的时候,还要重新配置一下 gnu 工具链.于是简单进行了安装了一下: yum install gcc yum install gcc-c++ yum install g ...

  6. celery 定时任务

    用celery定时任务,定时删除文件夹 tasks.py from celery import Celery import os import shutil app = Celery('demo') ...

  7. puppet 横向扩展(三)

    Table of Contents 1. 概述 2. 实验环境 3. 实验步骤 3.1. 机器B 的配置 3.2. 机器A 的配置 3.3. 测试配置结果 概述 横向扩展实验之三 – 将CA 认证服务 ...

  8. February 19th, 2018 Week 8th Monday

    Love is blind, hard to find, difficult to get, and impossible to forget. 爱,很盲目,很难找,很难得,很难忘. It is al ...

  9. Java断言(Assertion)

    断言(Assertion)是Java中一条语句,包含一个布尔表达式,当该布尔值为真,程序则被认为是正确的:当布尔值为假,则系统会抛出错误. 断言默认禁用的,在开发时候可开启功能,有利于纠正错误,增加可 ...

  10. 02.Python网络爬虫第二弹《http和https协议》

    一.HTTP协议 1.官方概念: HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文 ...