2016-12-13 7 views
1

I NSFetchResultControllerを使用してXMPPユーザーをフェッチしましたが、1つのセクションにオンラインユーザーを表示し、別のセクションにオフラインユーザーを表示しています。すべてのユーザーを結合することはできません。単一のtableviewセクションで表示する方法。iOS 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

fetchedResultsControllerをどのように作成しますか?フェッチされたエンティティをセクション単位で分割します。したがって、次のように簡単に作成することができます。

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

私はnumberOfRowsInSectionとnumberOfSectionsInTableに返さなければならないものを手伝ってくれてありがとうでしょうか。 。 –

+0

私はちょっと混乱しましたが、私の一日を救った魅力のように働いてくれてありがとう。 。 。 –

+0

あなたのコードでは、 'fetchedResultsController'のセクション数を返します。しかし、 'fetchedResultsController'初期化の変更のためにセクションを1つだけ変更したので、1セクションを返します。そのため、 'fetchedResultsController 'の変更は必要ありません。 – dosi

関連する問題