2016-12-13 1 views
1

I NSFetchResultController를 사용하여 XMPP 사용자를 가져 왔지만 하나의 섹션에 온라인 사용자를 표시하고 다른 섹션에 오프라인 사용자를 표시하면 모든 섹션을 하나의 섹션에 표시하고 싶습니다. 모든 사용자를 결합 할 수 없습니다. 단일 tableview 섹션에 표시하는 방법.iOS XMPP 테이블 뷰의 단일 섹션에 모든 사용자를 표시하는 방법

- (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 개 반환합니다. 그래서'fetchedResultsController '의 변경은 필요하지 않습니다. – dosi

관련 문제