【问题标题】:UITableView section to be sorted with one property and display another for the titleUITableView 部分将使用一个属性进行排序并为标题显示另一个
【发布时间】:2012-06-09 14:59:51
【问题描述】:

我有一个核心数据UITableView,核心数据托管对象有4个属性(headerTitle、headerNumber、objectTitle、objectComment)。

我希望 UITableView 由 headerNumber 分段,但 titleForHeaderInSection 是 headerTitle 属性。目前我在下面有这段代码....

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[__fetchedResultsController sections] objectAtIndex:section];
    return [NSString stringWithFormat:@"%@", [sectionInfo name]];
}

- (NSFetchedResultsController *)fetchedResultsController
{
    if (__fetchedResultsController != nil) {
        return __fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"headerNumber" cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
         // Replace this implementation with code to handle the error appropriately.
         // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return __fetchedResultsController;
} 

headerNumber 是 2012 年 6 月的数字年份和月份,如 201206。 headerTitle 是 headerNumber 的单词,例如“June 2012”

有人有什么建议吗?

【问题讨论】:

    标签: uitableview ios4 ios5


    【解决方案1】:

    您可以使用以下方法在表格视图中显示 headerTitle 属性

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    
    //return a view with uiLable and show your headerTitle there
    
    }
    

    并使用以下方法显示 headerNumber 属性

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
    {
          // put your code here 
    }
    

    希望对你有帮助

    【讨论】:

    • 谢谢...这确实有帮助。我还没有开始工作,但我认为我现在正朝着正确的方向前进。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    相关资源
    最近更新 更多