2015-10-16 2 views
0

В базовых данных есть два несвязанных объекта. Я хотел бы отобразить образцы этих объектов в двух разделах в одном UITableView (а также иметь возможность добавлять или удалять их из UITableView).Как отображать в отдельных разделах «tableview» несколько объектов из «coredata»?

Как реализовать эту задачу Используя один UIFetchedResultsController, возможно ли это сделать только с одним выбранным контроллером результатов?

UPDATE

Используйте 2 fetchedResultsControllers, но у меня есть ошибка:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[_PFArray objectAtIndex:]: index (1) beyond bounds (1)'

Это мой код реализации:

-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 

    if (indexPath.section == 0) { 
     Education* education = [self.fetchedResultsController1 objectAtIndexPath:indexPath]; 
     cell.textLabel.text = education.educationType; 
    } else { 
     FamilyStatus* familyStatus = [self.fetchedResultsController2 objectAtIndexPath:indexPath]; 
     cell.textLabel.text = familyStatus.familyStatusType; 
    } 
} 

#pragma mark - UITableViewDataSource - 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    if (section == 1) { 

     NSInteger rowsCount; 
     id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController1 sections] objectAtIndex:0]; 

     if (self.isEdit) { 

      rowsCount = [sectionInfo numberOfObjects] + 1; 

     } else { 

      rowsCount = [sectionInfo numberOfObjects]; 
     } 

     return rowsCount; 

    } else { 

     NSInteger rowsCount; 
     id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController2 sections] objectAtIndex:0]; 

     if (self.isEdit) { 

      rowsCount = [sectionInfo numberOfObjects] + 1; 

     } else { 

      rowsCount = [sectionInfo numberOfObjects]; 
     } 

     return rowsCount; 

    } 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString* cellIdentifier = @"Cell"; 
    static NSString* addCellIdentifier = @"CellAddButton"; 

    if (indexPath.section == 0) { 

     if (indexPath.row == 0 && self.isEdit) { 

      AddViewCell* cell = [tableView dequeueReusableCellWithIdentifier:addCellIdentifier]; 

      if (!cell) { 
       cell = [[AddViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:addCellIdentifier]; 
      } 

      return cell; 
     } 

     else { 

      UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

      if (!cell) { 
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
      } 

      if (self.isEdit) { 

       NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row-1 inSection:0]; 
       [self configureCell:cell atIndexPath:path]; 

      } else { 
       [self configureCell:cell atIndexPath:indexPath]; 
      } 

      return cell; 
     } 

    } else if (indexPath.section == 1) { 

     if (indexPath.row == 0 && self.isEdit) { 

      UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"aaa"]; 

      if (!cell) { 
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"aaa"]; 
      } 

      return cell; 
     } 

     else { 

      UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"bbb"]; 

      if (!cell) { 
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"bbb"]; 
      } 

      if (self.isEdit) { 

       NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row-1 inSection:0]; 
       [self configureCell:cell atIndexPath:path]; 

      } else { 

       [self configureCell:cell atIndexPath:indexPath]; 
      } 

      return cell; 
     } 
    } 

    return nil; 
} 

ответ

0

Вы не можете использовать один FRC, потому что это привязаны к одному объекту. Но вы можете использовать набор FRC, по одному для каждого объекта и по одному для каждого раздела. Вам нужно будет сделать некоторые манипуляции с указательными путями, потому что каждый FRC будет поставлять данные для раздела 0 таблицы.

+0

Я должен реализовать 5 геттеров, проверить номер раздела в numberOfRowsInSection и использовать другие методы по-разному для каждого раздела? – Georgij

+0

Или один многоразовый метод и небольшая конфигурация, или вкладки и 5 таблиц – Wain

+0

Спасибо. Но произошла ошибка. Что я делаю не так? – Georgij