UITableView分段与排序顺序不正确分段 [英] UITableView sectioned with sort order not sectioning correctly

查看:145
本文介绍了UITableView分段与排序顺序不正确分段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图得到一个UITableView显示一个对象的列表(节.title),但列出他们按日期的降序。



表格被拆分为正确标记部分(部分标题是不同的标题)。



但每个部分中的对象只有一半是正确的。



正在发生的一个例子:

 < Header>大标题名称
< data>< Big Title>< id = 1>< / data>
< data>< Big Title>< id = 4>< / data>
**< data>< Small Title>< id = 6>< / data> **< - 不应在此部分


< Header>小标题名称
< data><小标题>< id = 11>< / data>
< data>< Big Title>< id = 23>< / data> < - 不应该在此部分
**< data>< Small Title>< id = 66>< / data> **
/ pre>

这是我的代码的一部分:

   - NSFetchedResultsController *)fetchedResultsController {

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

/ *
设置获取的结果控制器。
* /
//为实体创建获取请求。
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
//根据需要编辑实体名称。
NSEntityDescription * entity = [NSEntityDescription entityForName:@AReadsinManagedObjectContext:[NSManagedObjectContext defaultContext]];
[fetchRequest setEntity:entity];

//将批量大小设置为合适的数量。
[fetchRequest setFetchBatchSize:20];

//使用timeStamp属性排序。
NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@timeStampascending:NO];
NSArray * sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor,nil];

[fetchRequest setSortDescriptors:sortDescriptors];

//使用sectionIdentifier属性分组到节中。
NSFetchedResultsController * aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[NSManagedObjectContext defaultContext] sectionNameKeyPath:@sessionTitlecacheName:@Root];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

return fetchedResultsController;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// return [ *)[masterSessions objectAtIndex:section] title];
id< NSFetchedResultsSectionInfo> theSection = [[fetchedResultsController sections] objectAtIndex:section];



NSString * theTitle = [theSection name];


返回Title;
}


解决方案

c $ c> sectionNameKeyPath ,并且在第一个排序描述符中使用的键必须是相同的键或者生成相同的相对排序。因此,您不能使用 sessionTitle 作为 sectionNameKeyPath 和完全不同的键 timeStamp 作为节的排序描述符。



在你的情况下,最好使用 timeStamp 作为 sectionNameKeyPath 描述符。

要显示 sessionTitle ,而不是 timeStamp ,则会显示 您可以修改 titleForHeaderInSection

   - (NSString * )tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
id< NSFetchedResultsSectionInfo> sectionInfo = [[self.controller sections] objectAtIndex:section];
return [[[SectionInfo objects] objectAtIndex:0] sessionTitle];
}


So I am trying to get a UITableView to show a list of objects by section (thing.title), but list them in descending order by date.

The table is is split into sections, which are labeled correctly (section headers are the different thing titles).

But the objects in each section are only half correct. The objects in each section are listed in descending order, but some sections contain data that should be in other sections.

An example of what is happening:

<Header> Big Title Name
    <data><Big Title><id=1></data>
    <data><Big Title><id=4></data>
    **<data><Small Title><id=6></data>**   <-- should not be in this section


<Header> Small Title Name
    <data><Small Title><id=11></data>
    <data><Big Title><id=23></data>  <-- should not be in this section
    **<data><Small Title><id=66></data>**

Here is part of my code:

- (NSFetchedResultsController *)fetchedResultsController {

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

/*
 Set up the fetched results controller.
 */
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"AReads" inManagedObjectContext:[NSManagedObjectContext defaultContext]];
[fetchRequest setEntity:entity];

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

// Sort using the timeStamp property..
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

// Use the sectionIdentifier property to group into sections.
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[NSManagedObjectContext defaultContext] sectionNameKeyPath:@"sessionTitle" cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

return fetchedResultsController;
}


- (NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:  (NSInteger)section
{
//return [(Sessions*)[masterSessions objectAtIndex: section] title];
id <NSFetchedResultsSectionInfo> theSection = [[fetchedResultsController sections] objectAtIndex:section];



NSString *theTitle = [theSection name];


return theTitle;
}

解决方案

The key used as sectionNameKeyPath of a fetched results controller and the key used in the first sort descriptor must either be the same keys or generate the same relative ordering. So you cannot use sessionTitle as sectionNameKeyPath and a completely different key timeStamp as sort descriptor for the sections.

In your case, it is probably the best to use timeStamp as sectionNameKeyPath and in the sort descriptor. This will ensure that all entries are correctly grouped into sections.

To display the sessionTitle instead of the timeStamp in the section header, you can modify titleForHeaderInSection:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.controller sections] objectAtIndex:section];
    return [[[sectionInfo objects] objectAtIndex:0] sessionTitle];
}

这篇关于UITableView分段与排序顺序不正确分段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆