【问题标题】:grouped table view and uisegmented control分组表视图和 uisegmented 控件
【发布时间】:2011-12-28 11:04:10
【问题描述】:

我有一个 UITableViewController 显示核心数据。我的数据实体有 5 个属性。我想在组表视图的部分中查看数据。使用 tableViewController 上的分段控件,我想在选择 A 段时按属性 A 查看它们,选择 B 时按属性 B,选择 C ​​时按 C。

澄清一下:假设属性 A 是“品牌”。 When the 'brand' segment is selected, I want to have the header for the group read 'BrandX' and then list grouped all the data that has a brand X, in the first grouped section.下一部分将标记为“BrandY”,依此类推。例如,对于“尺寸”和“材料”段也是如此。

因此,总而言之,一个表格视图控制器可以以三种不同的方式显示表格,由分段控件控制。

我尝试对 segmentedControl.selectedSegmentIndex 使用 switch 语句,然后对每种情况使用不同的 NSSortDescriptors。没用。

我尝试以类似的方式在 fetchedResultsController 中使用 if 语句。没有发生。

我只是没有看到这个。有人有想法吗?

谢谢

【问题讨论】:

    标签: objective-c ios5


    【解决方案1】:

    为了完成这项工作,您将在每次更改 SegmentControl 时重新创建 fetchedResultsController。为段控件设置一个 IBAction,它将:

    self.fetchedResultsController = nil; // destroys old one
    [self.tableView reloadData]; // tableview will reload and recreate new fetchedResultsController
    

    在 fetchedResultsController 中使用 selectedSegmentIndex 设置排序键,然后 然后使用它来设置排序描述符和部分名称keypath,并确保将缓存设置为nil。

    - (NSFetchedResultsController *)fetchedResultsController {
        ....
        NSString *sortKey = "according to current selected segment"; // set in a switch
        NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:YES];
        NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] 
                   initWithFetchRequest:fetchRequest 
                   managedObjectContext:self.managedObjectContext 
                     sectionNameKeyPath:sortKey cacheName:nil];
        ...
    }
    

    【讨论】:

    • 哇,埃里克。感谢您及时的回复。奇迹般有效。对我来说缺少的信息是我需要将 fetchedResultsController 设置为 nil。非常感谢。
    猜你喜欢
    • 2013-01-24
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多