继续上篇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. Android为TV端助力 关于线程的那些事

    今天发现之前自己一直有个误区,new Runnable(run()方法){}原来它不是一定创建一个线程 如果用主线程的handler去post(Runnable),他就不会创建子线程,而是在主线程上执 ...

  2. ReactNative调研结果

    React Native相关调研总结 一.概要 React Native - 使用React开发世界一流的原生应用: 使用JavaScript和React(对JS有一定扩展)作为开发语言: React ...

  3. Android-简单总结一下图片压缩

    最近项目需要用到图片压缩,所以简单总结一下.大致分为三种压缩. 图片质量压缩. 意思就是降低图片的质量,针对文件处理,但本身的像素点并不会减少. 本来像素点是这样的,经过算法计算,若一个像素点周围所存 ...

  4. nginx的rewrite ,如何在flask项目中获取重写前的url

    1. 在flask配一个重写到哪的路由,假设是/rewite/,然后到nginx的配置文件写重写规则,我这里重写全部的请求,接着测试能否重写成功 1. 添加一个路由 配置重写规则 测试成功 2.接下来 ...

  5. Spark数据倾斜及解决方案

    一.场景 1.绝大多数task执行得都非常快,但个别task执行极慢.比如,总共有100个task,97个task都在1s之内执行完了,但是剩余的task却要一两分钟.这种情况很常见. 2.原本能够正 ...

  6. Scala高阶函数实践

    高阶函数主要有两种:一种是将一个函数当做另外一个函数的参数(即函数参数):另外一种是返回值是函数的函数.package sparkCore/** * Created by zhen on 2018/3 ...

  7. jdk各版本特性

    JDK Version 1.0 开发代号为Oak(橡树),于1996-01-23发行. JDK Version 1.1 于1997-02-19发行. 引入的新特性包括: 引入JDBC(Java Dat ...

  8. C#面向对象 1

    using System; using System.Collections.Generic; using System.Collections; using System.Linq; using S ...

  9. 前后端分离djangorestframework——权限组件

    权限permissions 权限验证必须要在认证之后验证 权限组件也不用多说,读了源码你就很清楚了,跟认证组件很类似 具体的源码就不展示,自己去读吧,都在这里: 局部权限 设置model表,其中的ty ...

  10. 洗礼灵魂,修炼python(89)-- 知识拾遗篇 —— 进程

    进程 1.含义:计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位.说白了就是一个程序的执行实例. 执行一个程序就是一个进程,比如你打开浏览器看到我的博客,浏览器本身是一 ...