Search code examples
core-datansindexpath

Multiple index paths


I need to implement two different index paths on my tableview in cellForRow at index path... One with data from my fetched results controller and another for simple text.

What is the best way to go about this.


Solution

  • Im not exactly sure what your asking, but it sounds like you want a table with one section containing results from an NSFetchedResultController, and another section with data from some other source. This is easy to do. If your data can doesn't need to be in multiple sections then it's very easy, just get data from fetchedResultController for section 0, and something else for section 1.

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 2;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        if(section == 0){    
            id  sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
            return [sectionInfo numberOfObjects];
         }else{
            return self.someArray.Count;
         }
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
         if(indexpath.section == 0){    
              static NSString *CellIdentifier = @"data cell";
              UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
               NSObject *foo = [self.fetchedResultController objectAtIndexPath:indexPath];
               //configure cell
              return cell;
          }else{
              static NSString *CellIdentifier = @"some other cell";
              UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
               //configure cell
              return cell;
         }
    }