2011-03-12 53 views
1

我想添加兩個額外的行到我的UITableView。數據來自帶有部分的FetchResultsController。我已經嘗試了通常與數組一起工作的技巧,但他們沒有使用帶有節的FetchResultsController。只需在numberofrows中添加+2不會有幫助。NSFetchedResultCintroller與部分+ UITableView + 2多行

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return ([[fetchedResultsController sections] count]+2); 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section]; 
    return ([sectionInfo numberOfObjects]+2); 
} 

和fetchresultcontroller:

- (NSFetchedResultsController *)fetchedResultsController { 
    // Set up the fetched results controller if needed. 

    if (fetchedResultsController != nil) { 
     return fetchedResultsController; 
    } 

    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"eventsEntity" inManagedObjectContext:managedObjectContext]; 
    [request setEntity:entity]; 


    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"eventName" ascending:YES]; 
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 
    [request setSortDescriptors:sortDescriptors]; 
    [sortDescriptors release]; 
    [sortDescriptor release]; 

    NSFetchedResultsController *fetchedResultsController1 = 
    [[NSFetchedResultsController alloc] initWithFetchRequest:request 
             managedObjectContext:managedObjectContext 
              sectionNameKeyPath:@"eventName" cacheName:nil]; 


    self.fetchedResultsController = fetchedResultsController1; 
    fetchedResultsController.delegate = self; 

    [request release]; 
    [fetchedResultsController1 release]; 

    return fetchedResultsController; 
} 

回答

2

第一次嘗試獲得的不同概念graps:部分包含

所以,如果你想添加兩個,你可以將它們添加到現有的部分,或添加另一個部分,並在該部分放兩行。

這可能會是乾淨的解決方案,所以這裏的交易:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return ([[fetchedResultsController sections] count]+1); // +1 for your section 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    NSArray *sections = [fetchedResultsController sections]; 

    if (section < [sections count]) 
    { 
     // the normal case, e.g. sections 0,1,2 of section.count==3 
     id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:section]; 
     return [sectionInfo numberOfObjects]; 
    } else { 
     // your own section, e.g. the 4th section, where the FRC returned 3 sections 
     return 2; 
    } 
} 

當然,需要在返回細胞,標題,行高的方法,等等等等類似的修訂

+0

感謝mvds,現在我明白了...... – 2011-03-12 12:26:44