8.花名册
打开花名册头文件
XMPPRoster.h
XMPPRosterCoreDataStorage.h 核心数据存储
在appDelegate.h中定义XMPPRoster全局访问的花名册属性模块
在appDelegate.m中定义XMPPRosterCoreDataStorage数据存储模块
在设置XMPPStream中电子名片模块下实列化花名册并激活
2.3 _xmppRosterStorage = [[XMPPRosterCoreDataStorage alloc] init];
_xmppRoster = [[XMPPRoster alloc] initWithRosterStorage:_xmppRosterStorage];
3.重新连接模块添加到XMPPStream
[_xmppRoster activate:_xmppStream];
4.添加代理
[_xmppRoster addDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)];
//销毁XMPPStream并注销
{
1.删除代理
[_xmppRoster removeDelegate:self];
2.取消激活
[_xmppRoster deactivate];
4.内存清理
_xmppRoster = nil;
_xmppRosterStorage = nil;
}
创建一个类,RosterViewController继承UITableViewController 将花名册的class指向这个类 指定可重用标识符将identifier: RosterCell
使用XMPP花名册存储需要导入一个CoreData.framework框架
使用NSFetchedResultsController抓取结果控制器
在.m文件中导入<CoreData/CoreData.h>头文件
并定义一个成员变量NSFetchedResultsController *_fetchedResultsController;
// 表格分组数量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
return _fetchedResultsController.sections.cont; //返回查询结果的数量
}
//对应分组中表格的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *sectionData = [_fetchedResultsController sections];
if(sectionData.count>0){
id <NSFetchedResultsSectionInfo>sectionInfo = sectionData[section];
return [sectionInfo numberOfObjects];
}
return 0;
}
//表格行内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ID = @"RosterCell"; //ID为表格可重用标识符
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
return cell;
//设置单元格
}
// 实例化NSFetchedResultsController
导入代理头文件 设置appDelegate代理方法
-(AppDelegate *)appDelegate{
return [[UIApplication sharedApplication] delegate];
-(void)setupFetchedController{
0. 如果要针对coreData做数据访问,都离不开NSManagedObjectContext
1.实例化NSManagedObjectContext
NSManagedObjectContext *context = [[[self appDelegate] xmppRosterStorage]mainThreadManagedObjectContext];
2.实例化NSFetchRequest
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"XMPPUserCoreDataStorageObject"];
3.0实例化一个排序
NSSortDescriptor *sort1 = [NSSortDescriptor sortDescriptorWithKey:@"displayName" ascending:YES];
NSSortDescriptor *sort2 = [NSSortDescriptor sortDescriptorWithKey:@"jidStr" ascending:YES];
[request setSortDescriptors:@[sort1,sort2]];
3.实例化NSFetchedResultsController
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:@"sectionNum" cacheName:nil];
//设置FetchedResultsController的代理
[_fetchedResultsController setDelegate:self];
4.查询数据
NSError *error = nil;
if (![_fetchedResultsController performFetch:&error]){
NSLog(@"%@",error.localizedDescription);
};
}
//然后再到 设置单元格
XMPPUserCoreDataStorageObject *user = [_fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = user.displayName;