iOS XMPP如何在tableview

时间:2016-12-13 11:32:19

标签: ios xmpp

我使用NSFetchResultController获取了XMPP用户,但它在一个部分显示在线用户,在另一部分显示离线用户,我想在一个部分中显示所有用户,我无法将所有用户组合在一起。如何在单个tableview部分显示。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
 return [[[self fetchedResultsController] sections] count];
 }

 - (NSString *)tableView:(UITableView *)sender titleForHeaderInSection:(NSInteger)sectionIndex
 {
     NSArray *sections = [[self fetchedResultsController] sections];

     if (sectionIndex < [sections count])
     {
         id <NSFetchedResultsSectionInfo> sectionInfo = sections[sectionIndex];

         int section = [sectionInfo.name intValue];
         switch (section)
         {
             case 0  : return @"Available";
             case 1  : return @"Away";
             default : return @"Offline";
         }
     }

     return @"";
 }


 - (void)configurePhotoForCell:(UITableViewCell *)cell user:(XMPPUserCoreDataStorageObject *)user
 {
 if (user.photo != nil)
 {
 cell.imageView.image = user.photo;
 }
 else
 {
 NSData *photoData = [[[self appDelegate] xmppvCardAvatarModule] photoDataForJID:user.jid];

 if (photoData != nil)
 cell.imageView.image = [UIImage imageWithData:photoData];
 else
 cell.imageView.image = [UIImage imageNamed:@"defaultPerson"];
 }
 }


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
        NSArray *sections = [[self fetchedResultsController] sections];

        if (sectionIndex < [sections count])
        {
            id <NSFetchedResultsSectionInfo> sectionInfo = sections[sectionIndex];
            return sectionInfo.numberOfObjects;
        }

        return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell =(UITableViewCell *)[self.usersTable dequeueReusableCellWithIdentifier:@"users"];
    UILabel *nameLabel =(UILabel *)[cell viewWithTag:10];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;

    XMPPUserCoreDataStorageObject *user = [[self fetchedResultsController] objectAtIndexPath:indexPath];
    nameLabel.text = user.displayName;
    return cell;

}

1 个答案:

答案 0 :(得分:1)

如何创建fetchedResultsController?它负责按部分划分获取的实体。所以你可以按照以下方式创建它:

fetchedResultsController = [[NSFetchedResultsController alloc]
                                initWithFetchRequest:fetchRequest
                                managedObjectContext:self.managedObjectContext
                                sectionNameKeyPath:nil // pass nil here if you want single section
                                cacheName:nil];