table view cell又不显示了快来帮看看代码哪里不对了

iOS 码拜 8年前 (2016-03-26) 1096次浏览
#import "WCContactsViewController.h"
@interface WCContactsViewController ()
@property (nonatomic, strong) NSArray *friends;
@end
@implementation WCContactsViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 从数据里加载好友列表显示
    [self loadFriends];
}
-(void)loadFriends{
    //使用CoreData获取数据
    // 1.上下文【关联到数据库XMPPRoster.sqlite】
    NSManagedObjectContext *context = [WCXMPPTool sharedWCXMPPTool].rosterStorage.mainThreadManagedObjectContext;
    
    
    // 2.FetchRequest【查哪张表】
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"XMPPUserCoreDataStorageObject"];
    
    // 3.设置过滤和排序
    // 过滤当前登录用户的好友
    NSString *jid = [WCUserInfo sharedWCUserInfo].jid;
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"streamBareJidStr = %@",jid];
    request.predicate = pre;
    
    //排序
    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"displayName" ascending:YES];
    request.sortDescriptors = @[sort];
    
    // 4.执行请求获取数据
    self.friends = [context executeFetchRequest:request error:nil];
    NSLog(@"%@",self.friends);
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.friends.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"ContactCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 获取对应好友
    XMPPUserCoreDataStorageObject *friend =self.friends[indexPath.row];
    
    cell.textLabel.text = friend.jidStr;
    
    return cell;
}
@end
解决方案

40

self.friends  没有初始化和分配内存,不信打印一下,结果为空

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明table view cell又不显示了快来帮看看代码哪里不对了
喜欢 (0)
[1034331897@qq.com]
分享 (0)