OC中级通讯录

做一个班级信息程序,包含4个自定义的类:OurClass、Teacher、Student、
Person。
OurClass代表班级,⾥⾯包含一个Teacher对象,和一个NSMutableArray(用于存放多个Student对象)班级能够更换老师,能够增添学生和移除学生。Person类作为Student、Teacher类的父类,定义公共属性和⽅法。Student包含一个成绩属性(NSMutableDictionary类型),能存储课程名称和成绩,Student类还能比较年龄⼤小。Teacher类有一个exam:⽅法(参数是课程名称,内部是每个学生设置考试成绩,成绩随机)。

//———————————通讯录——————————
// MyAddressBook.h
// 中级通讯录
//
// Created by lanou on 15/10/31.
// Copyright (c) 2015年 JasperSong. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Contact.h"
@interface MyAddressBook : NSObject
// 要有一个存储所有信息的字典 字典key数组存放26个英文字母 value存放名字开头字母和这个字母相同的联系人数组
@property(retain,nonatomic)NSMutableDictionary *myDic; // 存储的字典

- (instancetype)init;

// 添加一个联系人
- (void)addContact:(Contact *)contact;

//按照首字母字母分组显示联系人
- (NSString *)showContactByFirstLetter:(NSString *)firstLetter;

//根据号码找联系人
- (NSString *)searchContactByPhoneNum:(NSString *)phoneNum;

//获取所有⼥性的联系⼈,并且按照年龄的降序排列。
- (NSString *)showFemaleContactDesc;

//根据姓名删除
- (void)removeContactByName:(NSString *)name;

// 删除某个分组
- (void)removeGroupByFirstLetter:(NSString *)firstLetter;

//根据分组显示
- (void)showAllContactByFirstLetter;
@end

//——————————————
// MyAddressBook.m
// 中级通讯录
//
// Created by lanou on 15/10/31.
// Copyright (c) 2015年 JasperSong. All rights reserved.
//

#import "MyAddressBook.h"

@implementation MyAddressBook
- (instancetype)init{
self = [super init];
if (self){

//存储联系人的字典初始化 共有26个键值对
_myDic = [NSMutableDictionary dictionary];
for (int i = 0; i < 26; i++){
[_myDic setObject:[NSMutableArray array] forKey:[NSString stringWithFormat:@"%c",'A'+i] ];
}
}
return self;
}

//判断是否合法联系人
- (BOOL)isRightContact:(Contact *)contact{
if (contact.name.length == 0||contact.phoneNum.length == 0){
return NO;
}
return YES;
}
// 添加一个联系人
- (void)addContact:(Contact *)contact{
if ([self isRightContact:contact]){
//截取联系人名字首字母 并大写
NSString *firstLetter = [contact.name substringWithRange:NSMakeRange(0, 1)].uppercaseString;
//判断加入哪个分组
for (NSString *obj in [_myDic allKeys]) {
if ([obj isEqualToString:firstLetter])
[_myDic[obj] addObject:contact];
}
}else{
NSLog(@"添加的联系人信息不全,添加失败!");
}
}

//showArray字符化
- (NSString *)showArrayDescription:(NSMutableArray *)showArray{
NSString *str = @"";
for (int i = 0; i < showArray.count; i++) {
Contact *c = showArray[i];
str = [str stringByAppendingString:[NSString stringWithFormat:@"\n %@",c]];
}
str = [str stringByAppendingString:@""];
return str;
}

//按照首字母字母分组显示联系人
- (NSString *)showContactByFirstLetter:(NSString *)firstLetter{
firstLetter = firstLetter.uppercaseString;//首个字母 大写形式
NSMutableArray *showArray = _myDic[firstLetter];
NSMutableString *str = [NSMutableString stringWithString:@""];
if (showArray.count != 0){
[str appendString: [NSMutableString stringWithString:firstLetter]];
[str appendString:[self showArrayDescription:showArray]];
[str appendString:@"\n"];
}
return str;
}

//根据号码找联系人
- (NSString *)searchContactByPhoneNum:(NSString *)phoneNum{
for (NSString *obj in [_myDic allKeys]) {
NSMutableArray *arr = _myDic[obj];
for (int i = 0; i < arr.count; i++){
if ([[arr[i] phoneNum] isEqualToString:phoneNum])
return [arr[i] description];
}
}
return @"";
}

//根据姓名删除
- (void)removeContactByName:(NSString *)name{
//汉字转拼音
NSMutableString *name2 = [NSMutableString stringWithString:name];
CFStringTransform((CFMutableStringRef)name2, NULL, kCFStringTransformToLatin, false);
CFStringTransform((CFMutableStringRef)name2, NULL, kCFStringTransformStripDiacritics, false);
NSString *name3 = [name2 stringByReplacingOccurrencesOfString:@" " withString:@""];
//从字典删除
NSString *firstLetter = [name3 substringWithRange:NSMakeRange(0, 1)].uppercaseString;
NSMutableArray *array = _myDic[firstLetter];
for (int i = 0; i < array.count; i++ ){
if ([[array[i] name] isEqualToString:name3]){
[array removeObject:array[i]];
i = -1; //为了删除更彻底 让i归0
}
}
}

// 删除某个分组
- (void)removeGroupByFirstLetter:(NSString *)firstLetter{
firstLetter = firstLetter.uppercaseString;
_myDic[firstLetter] = [NSMutableArray array];
}



//获取所有⼥性的联系⼈,并且按照年龄的降序排列。
- (NSString *)showFemaleContactDesc{
NSMutableArray *res = [NSMutableArray array];
//遍历字典及其value 找出所有的女性
for (NSString *obj in [_myDic allKeys]) {
NSArray *arr = _myDic[obj];
for (int i = 0; i < arr.count; i++){
if ([[arr[i] sex] isEqualToString:@"nu"]){
[res addObject:arr[i]];
}
}
}
//对结果数组冒泡排序
for (int i = 0; i < res.count - 1; i++){
for (int j = 0; j < res.count - 1 - i; j++){
if ([[res[j] age] intValue] < [[res[j+1] age] intValue] ){
Contact *c1 = [Contact contactWithContact:res[j]];
Contact *c2 = [Contact contactWithContact:res[j+1]];
[res replaceObjectAtIndex:j withObject:c2];
[res replaceObjectAtIndex:j+1 withObject:c1];
}
}
}
NSMutableString *str = [NSMutableString stringWithString:@"\n"];
for (Contact *obj in res) {
[str appendString:[obj description] ];
[str appendString:@"\n"];
}
return str;
}


//根据分组显示
- (void)showAllContactByFirstLetter{
NSMutableString *str = [NSMutableString stringWithString:@"\n"];
for (int i = 0; i < 26; i++){
NSString *sss = [NSString stringWithFormat:@"%c",'A'+i];
[str appendString: [self showContactByFirstLetter:sss]];
}
NSLog(@"%@",str);
}
@end
//---------------------
// AddressBook.h
// 中级通讯录
//
// Created by lanou on 15/10/30.
// Copyright (c) 2015年 JasperSong. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Contact.h"
@interface AddressBook : NSObject
// 要有一个存储所有信息的字典 字典key数组存放26个英文字母 value存放名字开头字母和这个字母相同的联系人数组
@property(retain,nonatomic)NSMutableDictionary *myDic; // 存储的字典
@property(retain,nonatomic)NSMutableDictionary *showDic; // 展示的字典
@property(retain,nonatomic)NSMutableArray *myArray; //所有的分组集合
@property(retain,nonatomic)NSMutableArray *showArray; //显示结果



- (instancetype)init;
- (NSString *)description;

// 添加一个联系人
- (void)addContact:(Contact *)contact;

//按照首字母字母分组显示联系人
- (NSString *)showContactByFirstLetter:(NSString *)firstLetter;

//根据号码找联系人
- (NSString *)searchContactByPhoneNum:(NSString *)phoneNum;

//获取所有⼥性的联系⼈,并且按照年龄的降序排列。
- (NSString *)showFemaleContactDesc;

//根据姓名删除
- (void)removeContactByName:(NSString *)name;

// 删除某个分组
- (void)removeGroupByFirstLetter:(NSString *)firstLetter;

//根据分组显示
- (void)showAllContactByFirstLetter;
@end

//----------------------
// AddressBook.m
// 中级通讯录
//
// Created by lanou on 15/10/30.
// Copyright (c) 2015年 JasperSong. All rights reserved.
//

#import "AddressBook.h"

@implementation AddressBook
- (instancetype)init{
self = [super init];
if (self){

//存储联系人的字典初始化 共有26个键值对
_myDic = [NSMutableDictionary dictionary];
for (int i = 0; i < 26; i++){
[_myDic setObject:[NSMutableArray array] forKey:[NSString stringWithFormat:@"%c",'A'+i] ];
}
//用于显示的字典
_showDic = [NSMutableDictionary dictionary];
//便于线性查找
_myArray = [NSMutableArray array];
}
return self;
}

- (NSString *)description{
return [super description];
}

//判断是否合法联系人
- (BOOL)isRightContact:(Contact *)contact{
if (contact.name.length == 0||contact.phoneNum.length == 0){
return NO;
}
return YES;
}
// 添加一个联系人
- (void)addContact:(Contact *)contact{
if ([self isRightContact:contact]){
[_myArray addObject:contact];
//截取联系人名字首字母 并大写
NSString *firstLetter = [contact.name substringWithRange:NSMakeRange(0, 1)].uppercaseString;
// NSLog(@"添加联系人第一个字母:%@",firstLetter);
//判断加入哪个分组
for (NSString *obj in [_myDic allKeys]) {
if ([obj isEqualToString:firstLetter])
// NSLog(@"%@",obj);
[_myDic[obj] addObject:contact];
}
}else{
NSLog(@"添加的联系人信息不全,添加失败!");
}

}
//showArray字符化
- (NSString *)showArrayDescription{
NSString *str = @"";
for (int i = 0; i < _showArray.count; i++) {
Contact *c = _showArray[i];
str = [str stringByAppendingString:[NSString stringWithFormat:@"\n %@",c]];
}
str = [str stringByAppendingString:@""];
return str;
}



//按照首字母字母分组显示联系人
- (NSString *)showContactByFirstLetter:(NSString *)firstLetter{
_showDic = [NSMutableDictionary dictionary];//注意.一定要清空 否则结果还是原来的
firstLetter = firstLetter.uppercaseString;
[_showDic setObject:_myDic[firstLetter] forKey:firstLetter];
NSMutableString *str = [NSMutableString stringWithString:firstLetter];
[str appendString:@"\n"];
NSMutableArray *arrContact = [_showDic allValues][0];
NSMutableArray *arrNames = [NSMutableArray array];
for (int i = 0; i < arrContact.count; i++){
[arrNames addObject: [arrContact[i] name]];
}
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjects:arrContact forKeys:arrNames];
NSArray *arr = [arrNames sortedArrayUsingSelector:@selector(compare:)];
// NSLog(@"%@",arrNames);
for (NSString* obj in arr) {
[str appendString:[dic[obj] description]];
[str appendString:@"\n"];
}
if (arr.count != 0){
return str;}
return @"";
}


//根据号码找联系人
- (NSString *)searchContactByPhoneNum:(NSString *)phoneNum{
[_showArray removeAllObjects];
_showArray = [NSMutableArray array];//注意:一定要这一步
for (int i = 0; i<_myArray.count; i++){
Contact *c=[_myArray objectAtIndex:i];
if([c.phoneNum isEqualToString:phoneNum]){
[_showArray addObject:c];
}

}
return [self showArrayDescription];
}


//获取所有⼥性的联系⼈,并且按照年龄的降序排列。
- (NSString *)showFemaleContactDesc{
[_showArray removeAllObjects];
_showArray = [NSMutableArray array];//注意:一定要这一步
for (int i = 0; i<_myArray.count; i++){
Contact *c=[_myArray objectAtIndex:i];
if([c.sex isEqualToString:@"f"]){
[_showArray addObject:c];
}
}
NSMutableArray *res = [NSMutableArray arrayWithArray:_showArray];
for (int i = 0; i < res.count - 1; i++){
for (int j = 0; j < res.count - 1 - i; j++){
if ([[res[j] age] intValue] < [[res[j+1] age] intValue] ){
Contact *c1 = [Contact contactWithContact:res[j]];
Contact *c2 = [Contact contactWithContact:res[j+1]];
[res replaceObjectAtIndex:j withObject:c2];
[res replaceObjectAtIndex:j+1 withObject:c1];
}
}
}
NSMutableString *str = [NSMutableString stringWithString:@"\n"];
for (Contact *obj in res) {
[str appendString:[obj description] ];
[str appendString:@"\n"];
}
return str;
}



//根据姓名删除
- (void)removeContactByName:(NSString *)name{
//从线性表删除
for (int i = 0; i<_myArray.count; i++){
Contact *c=[_myArray objectAtIndex:i];
if([c.name isEqualToString:name]){
[_myArray removeObject:c];
}
}
//从字典删除
NSString *firstLetter = [name substringWithRange:NSMakeRange(0, 1)].uppercaseString;
NSMutableArray *array = _myDic[firstLetter];
for (int i = 0; i < array.count; i++ ){
if ([[array[i] name] isEqualToString:name]){
[array removeObject:array[i]];
}
}
}

// 删除某个分组
- (void)removeGroupByFirstLetter:(NSString *)firstLetter{
firstLetter = firstLetter.uppercaseString;
_myDic[firstLetter] = [NSMutableArray array];
}

//根据分组显示
- (void)showAllContactByFirstLetter{
NSMutableString *str = [NSMutableString stringWithString:@"\n"];
for (int i = 0; i < 26; i++){
NSString *sss = [NSString stringWithFormat:@"%c",'A'+i];
[str appendString: [self showContactByFirstLetter:sss]];
}
NSLog(@"%@",str);
}
@end
//---------------
// Contact.h
// 通讯录
//
// Created by lanou on 15/10/29.
// Copyright (c) 2015年 JasperSong. All rights reserved.
//

#import <Foundation/Foundation.h>

// 联系人

@interface Contact : NSObject
@property(retain,nonatomic)NSString *name; //英文的姓名
@property(retain,nonatomic)NSString *nameC; //接受的中文名字
@property(retain,nonatomic)NSString *sex; //性别
@property(retain,nonatomic)NSString *sexC; //性别 中文
@property(retain,nonatomic)NSString *phoneNum; //电话号码
@property(retain,nonatomic)NSString *address; //住址
@property(retain,nonatomic)NSString *addressC;
@property(retain,nonatomic)NSString *arrayName; //分组名称
@property(retain,nonatomic)NSString *arrayNameC;
@property(retain,nonatomic)NSString* age; //年龄
- (instancetype)init;
+ (instancetype)contactWithContact:(Contact *)contact;

- (instancetype)initWithName:(NSString *)name
phoneNum:(NSString *)phoneNum;

- (instancetype)initWithName:(NSString *)name
phoneNum:(NSString *)phoneNum
sex:(NSString *)sex
address:(NSString *)address
arrayName:(NSString *)arrayName
age:(NSString *)age;


- (NSString *)description;
@end
//-------------
// Contact.m
// 通讯录
//
// Created by lanou on 15/10/29.
// Copyright (c) 2015年 JasperSong. All rights reserved.
//

#import "Contact.h"
@implementation Contact
//将中文转换成拼音
- (NSString *)ChineseToEglish:(NSString *)str{
NSMutableString *name = [NSMutableString stringWithString:str];
CFStringTransform((CFMutableStringRef)name, NULL, kCFStringTransformToLatin, false);
CFStringTransform((CFMutableStringRef)name, NULL, kCFStringTransformStripDiacritics, false);
NSString *newstr = [name stringByReplacingOccurrencesOfString:@" " withString:@""];
return newstr;
}
- (instancetype)init{
self = [super self];
if (self){
_arrayNameC = @"我的联系人";
_arrayName = @"myContact";
_age = @"1";
}
return self;
}
+ (instancetype)contactWithContact:(Contact *)contact{
Contact *p = [[Contact alloc]init];
p.name = contact.name;
p.sex = contact.sex;
p.phoneNum = contact.phoneNum;
p.address = contact.address;
p.arrayName = contact.arrayName;
p.age = contact.age;

p.nameC = contact.nameC;
p.sexC = contact.sexC;
p.addressC = contact.addressC;
p.arrayNameC = contact.arrayNameC;

return p;
}

- (instancetype)initWithName:(NSString *)name
phoneNum:(NSString *)phoneNum{
self = [super init];
if (self){
_nameC = name;
_phoneNum = phoneNum;
_name = [self ChineseToEglish:_nameC];
_sexC = @"不明";
_addressC = @"不明";
_age = @"不明";
_arrayNameC = @"我的联系人";
_arrayName = @"myContact";
}
return self;
}

- (instancetype)initWithName:(NSString *)name
phoneNum:(NSString *)phoneNum
sex:(NSString *)sex
address:(NSString *)address
arrayName:(NSString *)arrayName
age:(NSString *)age{
self = [super init];
if (self){
_nameC = name;
_sexC = sex;
_addressC = address;
_arrayNameC = arrayName;
_age = age;
_name = [self ChineseToEglish:_nameC];
_phoneNum = phoneNum;
_sex = [self ChineseToEglish:_sexC];
_address = [self ChineseToEglish:_addressC];
_arrayName = [self ChineseToEglish:_addressC];
}
return self;
}


- (NSString *)description{
NSString *str = [NSString stringWithFormat:@"姓名:%@ 性别: %@ 年龄: %@ 号码: %@ 住址: %@ 分组: %@ ",_nameC,_sexC,_age,_phoneNum,_addressC,_arrayNameC];
return str;

}
@end
//—————————————main.m——————————
// Contact.m
// 通讯录
//
// Created by lanou on 15/10/29.
// Copyright (c) 2015年 JasperSong. All rights reserved.
//

#import "Contact.h"
@implementation Contact
//将中文转换成拼音
- (NSString *)ChineseToEglish:(NSString *)str{
NSMutableString *name = [NSMutableString stringWithString:str];
CFStringTransform((CFMutableStringRef)name, NULL, kCFStringTransformToLatin, false);
CFStringTransform((CFMutableStringRef)name, NULL, kCFStringTransformStripDiacritics, false);
NSString *newstr = [name stringByReplacingOccurrencesOfString:@" " withString:@""];
return newstr;
}
- (instancetype)init{
self = [super self];
if (self){
_arrayNameC = @"我的联系人";
_arrayName = @"myContact";
_age = @"1";
}
return self;
}
+ (instancetype)contactWithContact:(Contact *)contact{
Contact *p = [[Contact alloc]init];
p.name = contact.name;
p.sex = contact.sex;
p.phoneNum = contact.phoneNum;
p.address = contact.address;
p.arrayName = contact.arrayName;
p.age = contact.age;

p.nameC = contact.nameC;
p.sexC = contact.sexC;
p.addressC = contact.addressC;
p.arrayNameC = contact.arrayNameC;

return p;
}

- (instancetype)initWithName:(NSString *)name
phoneNum:(NSString *)phoneNum{
self = [super init];
if (self){
_nameC = name;
_phoneNum = phoneNum;
_name = [self ChineseToEglish:_nameC];
_sexC = @"不明";
_addressC = @"不明";
_age = @"不明";
_arrayNameC = @"我的联系人";
_arrayName = @"myContact";
}
return self;
}

- (instancetype)initWithName:(NSString *)name
phoneNum:(NSString *)phoneNum
sex:(NSString *)sex
address:(NSString *)address
arrayName:(NSString *)arrayName
age:(NSString *)age{
self = [super init];
if (self){
_nameC = name;
_sexC = sex;
_addressC = address;
_arrayNameC = arrayName;
_age = age;
_name = [self ChineseToEglish:_nameC];
_phoneNum = phoneNum;
_sex = [self ChineseToEglish:_sexC];
_address = [self ChineseToEglish:_addressC];
_arrayName = [self ChineseToEglish:_addressC];
}
return self;
}


- (NSString *)description{
NSString *str = [NSString stringWithFormat:@"姓名:%@ 性别: %@ 年龄: %@ 号码: %@ 住址: %@ 分组: %@ ",_nameC,_sexC,_age,_phoneNum,_addressC,_arrayNameC];
return str;

}
@end

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可 6私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。
以下是一个简单的通讯录列表的OC代码实现: ``` // Contact.h #import <Foundation/Foundation.h> @interface Contact : NSObject @property (nonatomic, copy) NSString *name; @property (nonatomic, copy) NSString *phone; - (instancetype)initWithName:(NSString *)name phone:(NSString *)phone; @end // Contact.m #import "Contact.h" @implementation Contact - (instancetype)initWithName:(NSString *)name phone:(NSString *)phone { self = [super init]; if (self) { self.name = name; self.phone = phone; } return self; } @end // ContactCell.h #import <UIKit/UIKit.h> @interface ContactCell : UITableViewCell @property (nonatomic, strong) UILabel *nameLabel; @property (nonatomic, strong) UILabel *phoneLabel; @end // ContactCell.m #import "ContactCell.h" @implementation ContactCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 200, 20)]; [self.contentView addSubview:self.nameLabel]; self.phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 200, 20)]; [self.contentView addSubview:self.phoneLabel]; } return self; } @end // ContactListViewController.h #import <UIKit/UIKit.h> @interface ContactListViewController : UITableViewController @end // ContactListViewController.m #import "ContactListViewController.h" #import "Contact.h" #import "ContactCell.h" @interface ContactListViewController () @property (nonatomic, strong) NSMutableArray<Contact *> *contacts; @end @implementation ContactListViewController - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @"Contacts"; UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addContact)]; self.navigationItem.rightBarButtonItem = addButton; self.contacts = [NSMutableArray array]; Contact *contact1 = [[Contact alloc] initWithName:@"John Smith" phone:@"555-1234"]; [self.contacts addObject:contact1]; Contact *contact2 = [[Contact alloc] initWithName:@"Jane Doe" phone:@"555-5678"]; [self.contacts addObject:contact2]; [self.tableView registerClass:[ContactCell class] forCellReuseIdentifier:@"ContactCell"]; } #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.contacts.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ContactCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ContactCell" forIndexPath:indexPath]; Contact *contact = self.contacts[indexPath.row]; cell.nameLabel.text = contact.name; cell.phoneLabel.text = contact.phone; return cell; } #pragma mark - Actions - (void)addContact { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Add Contact" message:nil preferredStyle:UIAlertControllerStyleAlert]; [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"Name"; }]; [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"Phone"; }]; UIAlertAction *addAction = [UIAlertAction actionWithTitle:@"Add" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { UITextField *nameField = alert.textFields[0]; UITextField *phoneField = alert.textFields[1]; Contact *contact = [[Contact alloc] initWithName:nameField.text phone:phoneField.text]; [self.contacts addObject:contact]; [self.tableView reloadData]; }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]; [alert addAction:addAction]; [alert addAction:cancelAction]; [self presentViewController:alert animated:YES completion:nil]; } @end ``` 在这个实现中,我们创建了一个`Contact`类来表示每个联系人,一个`ContactCell`类来自定义通讯录列表中的单元格,一个`ContactListViewController`类来管理通讯录列表。我们使用`NSMutableArray`来存储所有的联系人,并在视图加载时向其中添加了一些示例联系人。 在`ContactListViewController`中,我们实现了`UITableViewDataSource`协议和`UITableViewDelegate`协议中的方法来显示通讯录列表和处理用户添加新联系人的操作。我们还添加了一个`UIBarButtonItem`,当用户点击它时会出现一个`UIAlertController`来允许用户输入新联系人的信息。最后,我们在`viewDidLoad`方法中注册了`ContactCell`类以供表视图使用。 这只是一个简单的例子,你可以根据自己的需要对其进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值