【问题标题】:NSFetchedResultCintroller with sections+ UITableView + 2 extra rowsNSFetchedResultCintroller 部分 + UITableView + 2 额外行
【发布时间】:2011-03-12 00:44:33
【问题描述】:

我正在尝试向我的 UITableView 添加两行额外的行。数据来自带有部分的 FetchResultsController。我已经尝试过通常适用于 Array 但它们不适用于 FetchResultsController 的技巧。简单地在 numberofrows 中添加 +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);
}

和 fetchresult 控制器:

- (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;
} 

【问题讨论】:

    标签: xcode uitableview nsfetchedresultscontroller rows


    【解决方案1】:

    首先尝试了解不同的概念:sections 包含 rows。

    因此,如果您想添加两个行,您可以将它们添加到现有部分,或者添加另一个部分并在该部分中放置两行。

    这可能是最干净的解决方案,所以这里是交易:

    - (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;
        }
    }
    

    当然,在返回单元格、标题、行高等的方法中也需要类似的修改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 2015-05-14
      • 2012-03-25
      • 2013-12-16
      相关资源
      最近更新 更多