iOS 定制手机通讯录(2)

建立BNRItemStore.h和BNRItemStore.m

sharedStore指针声明为静态变量,所指向的对象永远不会被释放。

allItems用来保存BNRItem对象,是NSArray类型,其他类不能修改它。

createItem用来创建对象,是NSMutableArray类型,可以增删。

createItem使用BNRItem定义的initItem,把原来的randomItem改了,新建时是空的,然后直接调出输入信息界面。

BNRItemStore.h

#import 

@class BNRItem;

@interface BNRItemStore : NSObject

@property (nonatomic, strong, readonly) NSArray *allItems;

+ (instancetype)sharedStore;
- (BNRItem *)createItem;

@end

BNRItemStore.m

#import “BNRItemStore.h”
#import “BNRItem.h”

@interface BNRItemStore()
@property (nonatomic) NSMutableArray *privateItems;

@end

@implementation BNRItemStore

+ (instancetype)sharedStore
{
static BNRItemStore *sharedStore = nil;

if (!sharedStore) {
sharedStore = [[self alloc] initPrivate];
}
return sharedStore;
}

- (instancetype)init
{
@throw [NSException exceptionWithName:@"Singleton"
reason:@"Use +[BNRItemStore sharedStore]”
userInfo:nil];
return nil;
}

- (instancetype)initPrivate
{
self = [super init];
if (self) {
_privateItems = [[NSMutableArray alloc] init];
}
return self;
}
- (NSArray *)allItems
{
return self.privateItems;
// return [self.privateItems copy];
}

- (BNRItem *)createItem
{
BNRItem *item = [BNRItem initItem];
[self.privateItems addObject:item];
return item;
}

@end

通讯录里要有初始的信息,所以在BNRItemStore除了实现createItem,还要实现createContacts方法,类似这样,不知道return什么,就return item1了

- (BNRItem *)createContacts
{
BNRItem *item1 = [[BNRItem alloc] initWithItemName:@”李雷”
NMTelCellPhone:@”18801767759″
NMTelLandLine:@”837″
NMTelContactEmail:@”huaen_zheng@niumag.com”];
[self.privateItems addObject:item1];
BNRItem *item2 = [[BNRItem alloc] initWithItemName:@”韩梅梅”
NMTelCellPhone:@”18801767759″
NMTelLandLine:@”837″
NMTelContactEmail:@”huaen_zheng@niumag.com”];
[self.privateItems addObject:item2];
BNRItem *item3 = [[BNRItem alloc] initWithItemName:@”小黄”
NMTelCellPhone:@”18801767759″
NMTelLandLine:@”837″
NMTelContactEmail:@”huaen_zheng@niumag.com”];
[self.privateItems addObject:item3];
BNRItem *item4 = [[BNRItem alloc] initWithItemName:@”小明”
NMTelCellPhone:@”18801767759″
NMTelLandLine:@”837″
NMTelContactEmail:@”huaen_zheng@niumag.com”];
[self.privateItems addObject:item4];
BNRItem *item5 = [[BNRItem alloc] initWithItemName:@”小豆豆”
NMTelCellPhone:@”18801767759″
NMTelLandLine:@”837″
NMTelContactEmail:@”huaen_zheng@niumag.com”];
[self.privateItems addObject:item5];
return item1;
}

然后把

[[BNRItemStore sharedStore] createContacts];

放到BNRItemsViewController的init方法里面,就可以在打开界面时初始化了。

UITableViewCell的实现
numberOfRowsInSection获取显示行数,别忘了把numberOfSectionsInTableView里边的0改成1
再用cellForRowAtIndexPath,创建UITableViewCell对象,获取allItem的第n个BNRItem对象,然后把BNRItem的描述信息赋给UITableViewCell对象的textLable,n是该UITableViewCell对象的表格行索引。

- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [[[BNRItemStore sharedStore] allItems] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell =
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@”UITableViewCell”];
NSArray *items = [[BNRItemStore sharedStore] allItems];
BNRItem *item = items[indexPath.row];
// NSLog(@”oh”);
cell.textLabel.text = [item description];

// Configure the cell…

return cell;
}

重用UITableViewCell对象,节省内存。
修改cellForRowAtIndexPath和viewDidLoad方法

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"
forIndexPath:indexPath];
NSArray *items = [[BNRItemStore sharedStore] allItems];
BNRItem *item = items[indexPath.row];
// NSLog(@”oh”);
cell.textLabel.text = [item description];

// Configure the cell…

return cell;
}

- (void)viewDidLoad {
[super viewDidLoad];

[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:@”UITableViewCell”];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值