iOS之通訊錄查看/添加/刪除等基本功能實現(xiàn)

為了方便大家觀看,我把每個文件里的代碼都用分割線分開, 并且在代碼中盡可能寫非常詳盡的注釋??


AppDelegate.m文件內(nèi)容

咱們先來創(chuàng)建self.window和根視圖控制器

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//定義我們工程整個的window
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

ViewController *vc = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
//將導航欄nav設置為根視圖控制器
self.window.rootViewController = nav;

[nav release];
[vc release];
[_window release];

return YES;
}

因為你創(chuàng)建了ViewController類型的對象,所以別忘了在頭文件引入ViewController.h哦.


ViewController.m文件內(nèi)容

#import "ViewController.h"
#import "DetailViewController.h"
#import "ADDFriendViewController.h"
//宏定義
#define IDENTIFIER @"WSScell"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>//簽署2個協(xié)議

@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *dataSourceArray;

@end

@implementation ViewController

-(void)dealloc
{
    [_tableView release];
    [_dataSourceArray release];
    [super dealloc];
}

這里我為大家一步步解釋:

#import "DetailViewController.h"
#import "ADDFriendViewController.h"

這是咱們的通訊錄需要用導航控制器navigationController來推出的兩個界面, 他們倆分別是聯(lián)系人詳情界面和添加聯(lián)系人(以及輸入詳細信息)界面

#define IDENTIFIER @"WSScell"

這是我們定義的一個宏,會便利于我們一會某個方法的書寫,下面會找到

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

這是簽署UITableView的兩個協(xié)議

剩下的部分是我們創(chuàng)建的兩個對象(表視圖, 數(shù)據(jù)源數(shù)組), 以及他們的釋放, 我相信小伙伴們這個都看的懂


- (void)viewDidLoad {
[super viewDidLoad];

//添加背景顏色
self.view.backgroundColor = [UIColor whiteColor];
//設置該界面(該ViewController)的標題
self.title = @"通訊錄";

//設置導航欄透明度為no
self.navigationController.navigationBar.translucent = NO;
//設置導航欄navigationBar顏色
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.33 green:0.33 blue:0.34 alpha:1.00];
//設置navigationBar的風格為黑色風格(這會將狀態(tài)欄顏色以及標題顏色變?yōu)榘咨?比較美觀
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

//右側(cè)添加按鈕(UIBarButton類型為Add,即添加)
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(didClickedRightButton:)];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

//左側(cè)添加按鈕(類型為系統(tǒng)類型editButtonItem,即編輯)
self.navigationItem.leftBarButtonItem = self.editButtonItem;

//我們在頭文件中簽署了兩個協(xié)議還記得吧,這里我們?yōu)閮蓚€協(xié)議分別設置代理人為self
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:IDENTIFIER];
}

我在代碼中幾乎每一行都加上了注釋,以便于大家的理解觀看. 上面最后一行是,注冊cell并且設置cell的重用標識符是@"WSScell",這里的@"WSScell"我們在頭文件定義過宏定義的

-(void)loadView {
[super loadView];
//創(chuàng)建搭載通訊錄的表視圖tableView, 并設置初始高度為50.0
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64) style:UITableViewStylePlain];
self.tableView.rowHeight = 50.0f;

[self.view addSubview:_tableView];
[_tableView release];

self.dataSourceArray = [NSMutableArray arrayWithContentsOfFile:@"/Users/dllo/Desktop/王少帥/UI/課后作業(yè)/王少帥10_UITableView/王少帥10_UITableView/Student.plist"];
}

這里我說一下, 最后一行這一大串子大家可能看不懂, 這里是一個Student.plist文件,里面放的是一個存放聯(lián)系人信息字典的數(shù)組, 每個字典有6個鍵值對,表示一個聯(lián)系人的整體信息, (因為文件不能上傳, 所以只能讓你們在這看), 如圖:

Snip20160413_1.png

接下來我們來看兩個因簽署了<UITableViewDataSource>協(xié)議而必須執(zhí)行的方法

//(1)返回值為創(chuàng)建的表視圖的行數(shù)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataSourceArray.count;
}
//(2)確定表視圖每個cell的內(nèi)容(這個方法每出現(xiàn)一個cell都走一次)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//根據(jù)重用標識符去重用池里找出對應cell, (重用池非常棒, 節(jié)省大量內(nèi)存)
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IDENTIFIER];
NSDictionary *dic = [self.dataSourceArray objectAtIndex:indexPath.row];
cell.textLabel.text = [dic valueForKey:@"name"];
return cell;
}

這個方法主要的核心在于創(chuàng)建一個字典dic接收數(shù)據(jù)源數(shù)組特定indexPath.row的元素,
并且將該cell的textField用dic的名字key賦值(就是在通訊錄第一頁上用表視圖顯示每個聯(lián)系人的名字.
.
以下是一些需要寫的關于tableView編輯的方法

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
//讓tableView處于可編輯狀態(tài)
[self.tableView setEditing:editing animated:animated];
}

.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
//設定全部cell都可編輯
return YES;
}
.
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//設置cell可支持的編輯樣式
return UITableViewCellEditingStyleDelete;
}

下面這個方法設置編輯完成的邏輯

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//判斷編輯的樣式(判斷是否是刪除樣式)
if (editingStyle == UITableViewCellEditingStyleDelete)
{
    //如果是刪除樣式,刪除數(shù)據(jù)源(數(shù)組對應的元素(字典))
    [self.dataSourceArray removeObjectAtIndex:indexPath.row];
    //刷新tableView
    [self.tableView reloadData];
}
}

下面為導航欄右側(cè)寫點擊方法(推出添加聯(lián)系人界面)

-(void)didClickedRightButton:(UIBarButtonItem *)button
{
ADDFriendViewController *addfvc = [[ADDFriendViewController alloc] init];
//利用block傳值, 將添加聯(lián)系人界面中的聯(lián)系人信息用字典dic傳回主界面
addfvc.box = ^(NSDictionary *dic)
{
    NSDictionary *dictionary = [NSDictionary dictionaryWithDictionary:dic];
    [self.dataSourceArray addObject:dictionary];
    [self.tableView reloadData];
    NSLog(@"%@", dic);
};
//推出添加聯(lián)系人界面
[self.navigationController pushViewController:addfvc animated:YES];
[addfvc release];
}

下面點擊cell推出該cell聯(lián)系人對應的詳情界面

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *dvc = [[DetailViewController alloc] init];
//推出詳情界面
[self.navigationController pushViewController:dvc animated:YES];
//創(chuàng)建一個字典接收從數(shù)據(jù)源數(shù)組指定下標取出的字典元素, 用該字典內(nèi)容布置詳細介面信息
dvc.detailDic = [self.dataSourceArray objectAtIndex:indexPath.row];
[dvc release];
}

DetailViewController.h 文件內(nèi)容

@interface DetailViewController : UIViewController
@property(nonatomic, retain)NSDictionary *detailDic;
@end

這里只有一步, 就是創(chuàng)建詳細信息字典這個屬性, 用來接收從主界面?zhèn)鬟^來的聯(lián)系人信息


DetailViewController.m 文件內(nèi)容

首先還是老問題了, 注意釋放(因為作者是在MRC可是下編程的)
- (void)dealloc
{
[_detailDic release];
[super dealloc];
}

在viewDidLoad中賦予背景顏色(在這里作者說一下, 如果新推出來一個界面不賦予背景顏色的話必然會在模擬器剛推出來的時候卡頓一下, 真機也是如此), 所以每一個新的界面第一步都先賦予白色背景顏色是一個很好地習慣哦
- (void)viewDidLoad
{
[super viewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];
//設置導航欄標題為在詳細信息字典dic中取出來的name對用的value, 也就是這個聯(lián)系人的名字
self.navigationItem.title = [_detailDic valueForKey:@"name"];
}
聯(lián)系人詳情界面導航欄特寫.png

下面是loadView里面走的程序

-(void)loadView
{
[super loadView];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
imageView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.00];
imageView.layer.cornerRadius = 50.0f;
imageView.layer.masksToBounds = YES;
imageView.image = [UIImage imageNamed:@"touxiang@2x"];
[self.view addSubview:imageView];
[imageView release];

UILabel *ageLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 140, 200, 30)];
ageLabel.text = [NSString stringWithFormat:@"年齡 : %@", [_detailDic valueForKey:@"age"]];
[self.view addSubview:ageLabel];
[ageLabel release];

UILabel *numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 180, 200, 30)];
numberLabel.text = [NSString stringWithFormat:@"學號 : %@", [_detailDic valueForKey:@"number"]];
[self.view addSubview:numberLabel];
[numberLabel release];

UILabel *genderLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 220, 200, 30)];
genderLabel.text = [NSString stringWithFormat:@"性別 : %@", [_detailDic valueForKey:@"gender"]];
[self.view addSubview:genderLabel];
[genderLabel release];

UILabel *photoLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 260, 200, 30)];
photoLabel.text = [NSString stringWithFormat:@"照片 : %@", [_detailDic valueForKey:@"photo"]];
[self.view addSubview:photoLabel];
[photoLabel release];

UILabel *hobbyLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 300, 200, 30)];
hobbyLabel.text = [NSString stringWithFormat:@"愛好 : %@", [_detailDic valueForKey:@"hobby"]];
[self.view addSubview:hobbyLabel];
[hobbyLabel release];
}

在loadView界面, 需要根據(jù)聯(lián)系人詳細字典, 搭載了包括name, age, number, gender, photo, hobby這6個對象,并按照比較美觀的樣式進行修改(這里全看個人審美), 重點是其中的內(nèi)容, 例如hobbyLabel的text就是根據(jù)聯(lián)系人詳細信息字典中hobby對應的value進行定義的,其他也是如此, 讓大家看一下界面效果:

聯(lián)系人詳情界面.png

到這里聯(lián)系人詳解界面基本就完成了, 點擊哪個cell, 該聯(lián)系人詳情界面的信息會根據(jù)傳過來的字典內(nèi)容進行賦值


ADDFriendViewController.h 文件內(nèi)容

typedef void(^MyBox)(NSDictionary *);

@interface ADDFriendViewController : UIViewController

@property(nonatomic, copy)MyBox box;

@end

這里我們定義了block屬性(為了進行block傳值), 并且對block進行重定義(為了方便書寫)

@interface ADDFriendViewController ()
@property(nonatomic, retain)UITextField *nameTextField;
@property(nonatomic, retain)UITextField *ageTextField;
@property(nonatomic, retain)UITextField *numberTextField;
@property(nonatomic, retain)UITextField *genderTextField;
@property(nonatomic, retain)UITextField *photoTextField;
@property(nonatomic, retain)UITextField *hobbyTextField;
@property(nonatomic, retain)NSDictionary *dic;
@end

聲明6個屬性
- (void)dealloc
{
Block_release(_box);
[_nameTextField release];
[_ageTextField release];
[_numberTextField release];
[_genderTextField release];
[_photoTextField release];
[_hobbyTextField release];
[_dic release];
[super dealloc];
}
release6個屬性

- (void)viewDidLoad {
[super viewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = @"添加聯(lián)系人";

_nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(87, 20, 200, 30)];
_nameTextField.borderStyle = UITextBorderStyleRoundedRect;
_nameTextField.placeholder = @"請輸入聯(lián)系人姓名";
[self.view addSubview:_nameTextField];
[_nameTextField release];

_ageTextField = [[UITextField alloc] initWithFrame:CGRectMake(87, 60, 200, 30)];
_ageTextField.borderStyle = UITextBorderStyleRoundedRect;
_ageTextField.placeholder = @"請輸入聯(lián)系人年齡";
[self.view addSubview:_ageTextField];
[_ageTextField release];

_numberTextField = [[UITextField alloc] initWithFrame:CGRectMake(87, 100, 200, 30)];
_numberTextField.borderStyle = UITextBorderStyleRoundedRect;
_numberTextField.placeholder = @"請輸入聯(lián)系人編號";
[self.view addSubview:_numberTextField];
[_numberTextField release];

_genderTextField = [[UITextField alloc] initWithFrame:CGRectMake(87, 140, 200, 30)];
_genderTextField.borderStyle = UITextBorderStyleRoundedRect;
_genderTextField.placeholder = @"請輸入聯(lián)系人性別";
[self.view addSubview:_genderTextField];
[_genderTextField release];

_photoTextField = [[UITextField alloc] initWithFrame:CGRectMake(87, 180, 200, 30)];
_photoTextField.borderStyle = UITextBorderStyleRoundedRect;
_photoTextField.placeholder = @"請設置聯(lián)系人照片";
[self.view addSubview:_photoTextField];
[_photoTextField release];

_hobbyTextField = [[UITextField alloc] initWithFrame:CGRectMake(87, 220, 200, 30)];
_hobbyTextField.borderStyle = UITextBorderStyleRoundedRect;
_hobbyTextField.placeholder = @"請輸入聯(lián)系人愛好";
[self.view addSubview:_hobbyTextField];
[_hobbyTextField release];

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(107, 280, 160, 20);
[button addTarget:self action:@selector(didClickedAddbutton:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"創(chuàng)建聯(lián)系人" forState:UIControlStateNormal];
[self.view addSubview:button];
}

以上是搭載這個界面, 這里不詳說
.
最主要的部分來了, 就是下面的這個, 為傳建聯(lián)系人按鈕添加點擊方法 ,block的逆向傳值, 并且傳的是一個字典dic

-(void)didClickedAddbutton:(UIButton *)button
{
//判斷添加的聯(lián)系人name欄是否為空
if ([self.nameTextField.text isEqualToString:@""])
{
    //如果name欄為空, 則彈出提示框提示
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提  示" message:@"聯(lián)系人姓名不能為空" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *actionIKnow = [UIAlertAction actionWithTitle:@"知道了??" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        nil;
    }];
    [alertController addAction:actionIKnow];
    [self showDetailViewController:alertController sender:nil];
}
else
{
    //如果name不為空,則可以創(chuàng)建該聯(lián)系人, 并且將該聯(lián)系人信息以字典的形式打包進block(我們的block名字為box), 并且將信息傳回主界面, 然后pop回前一頁(聯(lián)系人主頁面)
    self.dic = [NSDictionary dictionaryWithObjectsAndKeys: self.nameTextField.text, @"name", self.ageTextField.text, @"age", self.numberTextField.text, @"number", self.genderTextField.text, @"gender", self.photoTextField.text, @"photo", self.hobbyTextField.text, @"hobby", nil];
    [self.nameTextField retain];
    [self.ageTextField retain];
    [self.genderTextField retain];
    [self.photoTextField retain];
    [self.numberTextField retain];
    [self.hobbyTextField retain];
    self.box(self.dic);
    [self.navigationController popViewControllerAnimated:YES];
}
}

創(chuàng)建聯(lián)系人界面.png
通訊錄主頁.png

如果添加空name的聯(lián)系人, 則會彈出警告提示框


警告提示框.png

聯(lián)系人主界面, 左上角Edit按鈕, 點擊后可進入選擇刪除

聯(lián)系人主界面, 左上角Edit按鈕.png
點擊Edit按鈕后, 可進行聯(lián)系人的刪除操作.png

真的是寫的詳盡的不能再詳盡了, 寫的可能有的地方摻雜了個人觀念, 畢竟不是IOS真正的大神, 理解不是特別滲透, 不過希望我的用心能幫助大家??

zhi

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,527評論 6 544
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,687評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,640評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,957評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,682評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,011評論 1 329
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,009評論 3 449
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 43,183評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,714評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 41,435評論 3 359
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,665評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,148評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,838評論 3 350
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,251評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,588評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,379評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,627評論 2 380

推薦閱讀更多精彩內(nèi)容

  • 資料收集自網(wǎng)絡... 1.通訊錄? 學習目的:控制器跳轉(zhuǎn),控制器之間傳值,數(shù)據(jù)存儲2.項目演示:(4個界面,交給4...
    b485c88ab697閱讀 314評論 0 0
  • *面試心聲:其實這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個offer,總結(jié)起來就是把...
    Dove_iOS閱讀 27,199評論 30 471
  • 2017.02.22 可以練習,每當這個時候,腦袋就犯困,我這腦袋真是神奇呀,一說讓你做事情,你就犯困,你可不要太...
    Carden閱讀 1,366評論 0 1
  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,179評論 4 61
  • 這個系列從去年開始斷斷續(xù)續(xù)地寫,今年要繼續(xù)寫下去,爭取寫到100輯。 把這個系列在豆瓣上做了一個豆列,如果想看之前...
    柳二白閱讀 260評論 0 3