一:在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. C#中操作txt,抛出“正由另一进程使用,因此该进程无法访问此文件”

    将你的File.Create(fileName); //创建fileName路径的文本改为 1 2 3 using (FileStream fs = File.Create(fileName)){} ...

  2. python 练习 6

    #!/usr/bin/python # -*- coding: utf-8 -*- from collections import deque from math import log10 def p ...

  3. cssText在 IE6/7/8和chrome/Firefox/IE9+的不同

    一,结尾没有分号 <div style="font-size:14px;">test</div> <script type="text/ja ...

  4. uva--1339 - Ancient Cipher(模拟水体系列)

    1339 - Ancient Cipher Ancient Roman empire had a strong government system with various departments, ...

  5. Binary Tree Level Order Traversal [LeetCode]

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  6. 数据库迁移 Migrator.Net

    转载: [强烈推荐]数据库迁移利器:Migrator.Net Migrator.NET:https://github.com/migratordotnet/Migrator.NET fluentmig ...

  7. yii框架

    Yii Framework是一个基于组件.用于开发大型 Web 应用的高性能 PHP 框架.Yii提供了今日Web 2.0应用开发所需要的几乎一切功能.Yii是最有效率的PHP框架之一.Yii是创始人 ...

  8. Windows Store App 应用设置存储

    应用设置存储主要分为本地存储和漫游存储两种方式.本地存储是指将应用的设置信息存储在本地存储空间中,而漫游存储则是指将应用的设置信息存储在网络服务器中,相对于本地存储而言,漫游存储支持多台设备之间的应用 ...

  9. linux copy

    cp -ri 131115-6/* /web/www/attachment/  把13这个文件夹下面所有的文件和文件夹复制到 /web/www/attachment cp -a  a  /web/ww ...

  10. Linux下配置用msmtp和mutt发邮件

    Linux下可以直接用mail命令发送邮件,但是发件人是user@servername,如果机器没有外网的dns,其他人就无法回复.此时,有一个可以使用网络免费邮箱服务的邮件发送程序就比较重要了.ms ...