一:在viewDidLoad方法中代码添加一个UIBarButtonItem,并将其的类型设置成垃圾桶,代码如下:

- (void)viewDidLoad {
[super viewDidLoad];
self.title = [NSString stringWithFormat:@"%@的联系人",self.name];
// 增加一个删除联系人的按钮
UIBarButtonItem *deleteItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(delete)];
[self.navigationItem setRightBarButtonItems:@[deleteItem,self.navigationItem.rightBarButtonItem]];
}

二:实现delete方法,并重写表格编辑状态的setter方法

 - (void)delete
{
// 设置表格的编辑属性
[self.tableView setEditing:!self.tableView.editing animated:YES];
}
#pragma mark - 重写表格的编辑状态的setter方法
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%s!!!!!!!!",__func__);
if (indexPath.row == ) {
// 当行号为0时,返回添加按钮状态
return UITableViewCellEditingStyleInsert;
}
// 当行号不为0时,返回删除按钮状态
return UITableViewCellEditingStyleDelete;
}

三:当用户点击编辑状态下的按钮时,实现删除联系人或者增加联系人

 //点击 编辑状态表格的按钮 系统会调用的方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// 判断用户点击的按钮类型
if (editingStyle == UITableViewCellEditingStyleDelete) {
// 在数组中删除该联系人
//1.先获取该联系人
NSInteger pathOfRow = indexPath.row;
[self.contacts removeObject:self.contacts[pathOfRow]];
// 刷新表格
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
// 同步沙盒数据
[NSKeyedArchiver archiveRootObject:self.contacts toFile:self.contactPath];
}
// 当用户点击增加按钮事件时
if (editingStyle == UITableViewCellEditingStyleInsert) {
contact *con = [[contact alloc] init];
con.name = @"赵小姐";
con.tel = @"";
[self.contacts addObject:con];
NSIndexPath *path = [NSIndexPath indexPathForItem:indexPath.row + inSection:];
[self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationLeft];
[NSKeyedArchiver archiveRootObject:self.contacts toFile:self.contactPath];
}
}

四 实际效果:

2016-1-7第一个完整APP 私人通讯录的实现 6:在联系人界面增加删除联系人的功能的更多相关文章

  1. 2016-1-5第一个完整APP 私人通讯录的实现 1:登录界面及跳转的简单实现2

    ---恢复内容开始--- 实际效果如上 一:Segue的学习 1.什么是Segue: Storyboard上每一根用来界面跳转的线,都是一个UIStoryboardSegue对象(简称Segue)   ...

  2. 2016-1-7第一个完整APP 私人通讯录的实现 5:保存数据

    一:登陆界面 1):用户点击登陆按钮并成功登陆后,根据此时的开关情况选择是否保存数据,代码如下: "]) { [self performSegueWithIdentifier:@" ...

  3. 2016-1-6第一个完整APP 私人通讯录的实现 4:编辑联系人

    一:建立编辑联系人的controller,并使其拥有模型contact,且有协议.代码如下 #import <UIKit/UIKit.h> #import "contact.h& ...

  4. 2016-1-6第一个完整APP 私人通讯录的实现 3:添加联系人

    一:创建模型对象:contact用于存放数据,也便于读取加载 #import <Foundation/Foundation.h> @interface contact : NSObject ...

  5. 2016-1-6第一个完整APP 私人通讯录的实现 2:增加提示用户的提示框,监听文本框

    一:在登录时弹出提示用户的提示框: 1.使用第三方框架. 2.在登陆按钮点击事件中增加如下代码: - (IBAction)loginBtnClicked { NSString *acount = se ...

  6. iOS完整App资源收集

    前言 iOS开发学习者都希望得到实战训练,但是很多资料都是只有一小部分代码,并不能形成完成的App,笔者在此处收集了很多开源的完整的App,都有源代码哦! 本篇文章持续更新中,请持续关注.本篇所收集的 ...

  7. MUI框架开发HTML5手机APP(一)--搭建第一个手机APP

      前  言 JRedu 随着HTML5的不断发展,移动开发成为主流趋势!越来越多的公司开始选择使用HTML5开发手机APP,而随着手机硬件设备配置的不断提升,各种开发框架的不断优化,也使着H5开发的 ...

  8. MUI框架开发HTML5手机APP(一)--搭建第一个手机APP(转)

    出处:http://www.cnblogs.com/jerehedu/p/7832808.html  前  言 JRedu 随着HTML5的不断发展,移动开发成为主流趋势!越来越多的公司开始选择使用H ...

  9. 浅谈MAIC 2016第二届移动应用(APP)创新大会

    MAIC 2016第二届移动应用(APP)创新大会将于2016年12月在上海举办!MAIC一届比一届办的有质量,规模越大.今年也如约而至,预计今年MAIC规模逾2000人.大会以专业会议,创新应用展览 ...

随机推荐

  1. VMware Workstation 12 Pro虚拟机下载(含序列号)

    VMware Workstation 12 Pro 官网下载地址:http://www.vmware.com/products/workstation/workstation-evaluation V ...

  2. css slice和splice

    slice()方法从已有的数组中返回选定元素: slice(start,end)start:必需.规定从何处开始选取.如果是负数,那么它规定从数组尾部开始算起的位置.也就是说,-1 指最后一个元素,- ...

  3. css3内容持续更新ing

    要做在手机里面.4s和5都有差别更加何况还有其他安卓手机,所以做好之后要缩小一点 用的是background.用的了background-size,如果是50%就是等比缩放,50% 50%就会变形,现 ...

  4. js--题目二

    //如何获取下面代码所有元素. <form> <input name="" type="text" /> <input name= ...

  5. Shell基础:常用技巧&重定向&管道操作

    Shell脚本介绍和常用工具 Shell脚本 Shell脚本:实际就是windows里的批处理脚本,多条可一次执行的Shell命令集合.Linux上的脚本可以用很多种语言实现,bash shell是比 ...

  6. zju 1091

    // Traveling Knight Problem #include "stdafx.h" #include <string> #include <strin ...

  7. [转]ebkit内核浏览器的Linear Gradients (线性渐变)

    转自:http://www.css88.com/archives/tag/webkit-gradient webkit内核的safari. Chrome的Linear Gradients (线性渐变) ...

  8. HDUOJ----2571(命运)(简单动态规划)

    命运 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  9. 用SQL语句操作数据库

    —―有一天,当你发觉日子特别的艰难,那可能是这次的收获将特别的巨大.—―致那些懈怠的岁月 本章任务: 学生数据库中数据的增加.修改和删除 目标: 1:使用T-SQL向表中插入数据 2:使用T-SQL更 ...

  10. netbios wins dns LLMNR

    NetBIOS名称 Network Basic Input/Output System  (RFC-1001,1002)网络基本输入/输出系统协议 NetBIOS是一种高级网络接口,最初是在硬件中实 ...