继续上篇UITableView和UITableViewController

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

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

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

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

  1. - (IBAction)addNewItem:(id)sender {
  2.  
  3. }
  4.  
  5. - (IBAction)toggleEditingMode:(id)sender {
  6.  
  7. }

  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文件:

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

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

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

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
  4.  
  5. UIView *header = self.headerView;
  6. [self.tableView setTableHeaderView:header];
  7. }

  运行程序,结果为:

  修改toggleEditingMode:方法如下:

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

  运行程序,结果为:

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

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

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

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

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

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

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

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

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

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

移动行:

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

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

在BNRitem.m的实现代码为:

  1. - (void)moveItemAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex {
  2. if (fromIndex == toIndex) {
  3. return;
  4. }
  5. BNRItem *item = self.privateItems[fromIndex];
  6. [self.privateItems removeObjectAtIndex:fromIndex];
  7. [self.privateItems insertObject:item atIndex:toIndex];
  8. }

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

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

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

运行结果如下:

程序代码: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. iOS ----------字符串处理

    //一.NSString/*----------------创建字符串的方法----------------*/ //1.创建常量字符串.NSString *astring = @"This ...

  2. Android 底部导航栏实现一 Fragment-replace

    [效果](这里下载的软件收费的试用有水印) [推荐]这里推荐一个图标网http://iconfont.cn/.以上图标来自此图标网 [项目结构] [步骤] ①创建布局文件,写底部导航栏 <?xm ...

  3. SQL Server 2005 sp_send_dbmail出现Internal error at FormatRowset (Reason: Not enough storage is available to complete this operation)

    案例环境: 操作系统: Windows 2003 SE 32bit(SP2) 数据库版本:Microsoft SQL Server 2005 - 9.00.5069.00 (Intel X86) Au ...

  4. springmvc复习笔记----Restful 风格,PathVariable获取 Url实例

    结构 包与之前相同 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&qu ...

  5. [20190213]测试服务端打开那些端口.txt

    [20190213]测试服务端打开那些端口.txt --//前几天测试使用发送信息到/dev/tcp/ip_address/port,测试端口是否打开.写简单写一个脚本验证看看. $ seq 1 65 ...

  6. C#-简介(一)

    1.C#语言简介 C#计算机语言 是一门高级计算机语言 他的开发模式更接近人类和社会的思维模式,有助于提高开发效率 C#历史 1998年COOL这个项目是C#语言的前身,由微软 Anders Hejl ...

  7. selenium驱动程序下载和使用流程

    转自https://blog.csdn.net/weixin_42660771/article/details/81286982 1.下载地址    https://github.com/mozill ...

  8. TNS-12535: TNS:operation timed out

    AWS数据库云服务器出现了连接超时的错误,于是查看相关时段的alert日志,发现了如下的错误: **************************************************** ...

  9. Python PEP-8编码风格指南中文版

    #PEP 8 – Python编码风格指南 PEP: 8 Title: Style Guide for Python Code Author: Guido van Rossum , Barry War ...

  10. MySQL Host is blocked because of many connection errors 解决方法

    应用日志提示错误:create connection error, url: jdbc:mysql://10.45.236.235:3306/db_wang?useUnicode=true&c ...