gpt4 book ai didi

iOS 索引 TableView 使用核心数据和排序 - 索引可以,但数据不行

转载 作者:行者123 更新时间:2023-11-28 21:58:45 25 4
gpt4 key购买 nike

我需要一些帮助来索引 UITableView,将数据分成字母部分。数据模型来自核心数据图。

我有索引 (A-Z),加载正常,节标题正确。问题是数据未正确排序(即按字母顺序排序)。

模型中有一个实体属性是字母索引。我查看了 sqlite 文件,没问题。

这里是一些相关的代码。请帮助我了解我遗漏或弄乱的东西:)

- (void)viewDidLoad {
[super viewDidLoad];

sectionArray = [[NSArray alloc] init];
self.collation = [UILocalizedIndexedCollation currentCollation];

if (languageKey == 0) {
sectionArray = [NSArray arrayWithArray:[@"|α,ά|β|γ|δ|ε,έ|ζ|η,ή|θ|ι,ί|κ|λ|μ|ν|Ξ|ο,ό|π|ρ|σ|τ|υ,υ|φ|χ|ψ|ω,ώ|#"
componentsSeparatedByString:@"|"]];

} else {
sectionArray = [NSArray arrayWithArray:
[@"A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|#"
componentsSeparatedByString:@"|"]];
}

NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"WordEntity" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
[request setPredicate:[self predicate]];
[request setIncludesSubentities:NO];

filteredWordsArray = [[NSMutableArray alloc] init];
self.searchResults = [NSMutableArray arrayWithCapacity:[[self.fetchedResultsController fetchedObjects] count]];

self.searchDisplayController.searchResultsTableView.rowHeight = wordTable.rowHeight;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return [self.searchResults count];
}
else
{
return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"WordCell";
UITableViewCell *cell = (UITableViewCell *)[self.wordTable dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

WordEntity *word = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
word = [self.searchResults objectAtIndex:indexPath.row];
count = self.searchResults.count;
self.numberWordsLabel.text = [NSString stringWithFormat:@"%lu", (unsigned long)count];

} else {

word = [self.fetchedResultsController objectAtIndexPath:indexPath];
self.numberWordsLabel.text = [NSString stringWithFormat:@"%lu", (unsigned long)fullCount];
}


if (languageKey == 0) {
cell.textLabel.text = word.greekText;
cell.detailTextLabel.text = word.englishText;
} else {
cell.textLabel.text = word.englishText;
cell.detailTextLabel.text = word.greekText;
}

return cell;
}

/*
Section-related methods: Retrieve the section titles and section index titles from the collation.
*/


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
long languageKey = [defaults integerForKey:DEFAULT_KEY_LANGUAGE_NUMBER];
long count = 0;
if (languageKey == 0) {
count = 24;
} else {
count = 26;
}
return count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [sectionArray objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return sectionArray;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [collation sectionForSectionIndexTitleAtIndex:index];
}

- (NSFetchedResultsController *)fetchedResultsController {

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] init];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
long languageKey = [defaults integerForKey:DEFAULT_KEY_LANGUAGE_NUMBER ];
if (languageKey == 0) {
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"greekKey" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
// sectionTitleString = @"greekKey";
} else {
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"englishKey" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
// sectionTitleString = @"englishKey";
}

NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];

_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"greekKey" cacheName:nil];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}

/*
-(NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector

{
collation = [UILocalizedIndexedCollation currentCollation];

NSInteger sectionCount = [[collation sectionTitles] count]; //section count is from sectionTitles and not sectionIndexTitles
NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount];

//create an array to hold the data for each section
for(int i = 0; i < sectionCount; i++)
{
[unsortedSections addObject:[NSMutableArray array]];
}

//put each object into a section
for (id object in array)
{
NSInteger index = [collation sectionForObject:object collationStringSelector:selector];
[[unsortedSections objectAtIndex:index] addObject:object];
}

sections = [NSMutableArray arrayWithCapacity:sectionCount];

//sort each section
for (NSMutableArray *section in unsortedSections)
{
[sections addObject:[collation sortedArrayFromArray:section collationStringSelector:selector]];
}

return sections;
}
*/

这是我看到的:

enter image description here

最佳答案

尝试将排序描述符添加到您的 NSFetchRequest...

NSSortDescriptor *sortDescriptorSecondary = [[NSSortDescriptor alloc]
initWithKey:@"word" ascending:YES];
[request setSortDescriptors:@[sectionArray, sortDescriptorSecondary]];

请注意,当您在表格 View 中使用部分时,您必须始终先按部分排序,然后再按其他条件排序。


更新

更详细一点...

要包括的私有(private)属性:

@property (nonatomic, strong) NSArray *sectionArray;

获取请求包括:

    //  Declare sort descriptors
NSArray *requestSortDescriptors = nil;
NSSortDescriptor *sortDescriptorPrimary = nil;
NSSortDescriptor *sortDescriptorSecondary = nil;

// Set sort descriptors...
// Primary sort descriptor is your section array - sort by sections first.
sortDescriptorPrimary = [NSSortDescriptor sortDescriptorWithKey:self.sectionArray ascending:YES];
// Secondary sort descriptor is your entity attribute `word` - sort by fetched data second.
sortDescriptorSecondary = [NSSortDescriptor sortDescriptorWithKey:@"word" ascending:YES];

// Set sort descriptor array
requestSortDescriptors = @[sortDescriptorPrimary, sortDescriptorSecondary];

// Apply sort descriptors to fetch request
[request setSortDescriptors:requestSortDescriptors];

希望这对您有所帮助,如果您需要进一步的解释,请告诉我。


第二次更新

我忽略了如何传播部分数据。

就我个人而言,对于要在带有节标题的 TVC 中显示的数据,我为每个实体定义了一个实体属性,为简单起见,我始终将其称为 sectionIdentifier

然后,作为我持久化数据方法的一部分,我确保将“部分”数据分配给此属性 sectionIdentifier(例如,在您的示例 A、B、C 等中)。

然而,在您的情况下,对于这个特定示例,您已经在 viewDidLoad TVC 生命周期方法中建立了一个名为 sectionArray 的局部变量。

考虑到您的代码,我建议如下(根据上述)...

要包括的私有(private)属性:

@property (nonatomic, strong) NSArray *sectionArray;

同时考虑到您的代码,我建议以下(新)...

改变您的 TVC 生命周期方法 viewDidLoad:

- (void)viewDidLoad {
[super viewDidLoad];

self.sectionArray = nil;
self.collation = [UILocalizedIndexedCollation currentCollation];

if (languageKey == 0) {
self.sectionArray = [NSArray arrayWithArray:[@"|α,ά|β|γ|δ|ε,έ|ζ|η,ή|θ|ι,ί|κ|λ|μ|ν|Ξ|ο,ό|π|ρ|σ|τ|υ,υ|φ|χ|ψ|ω,ώ|#"
componentsSeparatedByString:@"|"]];

} else {
self.sectionArray = [NSArray arrayWithArray:[@"A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|#"
componentsSeparatedByString:@"|"]];
}

// I have commented out code following because I cannot see where you are using it.
// Does the compiler not throw up warnings for this fetch request?
//
// NSManagedObjectContext *context = [self managedObjectContext];
// NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"WordEntity" inManagedObjectContext:context];
// NSFetchRequest *request = [[NSFetchRequest alloc] init];
// [request setEntity:entityDescription];
// [request setPredicate:[self predicate]];
// [request setIncludesSubentities:NO];

filteredWordsArray = [[NSMutableArray alloc] init];
self.searchResults = [NSMutableArray arrayWithCapacity:[[self.fetchedResultsController fetchedObjects] count]];

self.searchDisplayController.searchResultsTableView.rowHeight = wordTable.rowHeight;
}

同时考虑到您的代码,我建议以下(新)...

更改您的 NSFetchedResultsController getter 方法:

- (NSFetchedResultsController *)fetchedResultsController {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
long languageKey = [defaults integerForKey:DEFAULT_KEY_LANGUAGE_NUMBER ];

// Set up fetch request
NSFetchRequest *fetchRequest = [[NSFetchRequest fetchRequestWithEntityName:@"WordEntity"];
[fetchRequest setPredicate:[self predicate]];
[fetchRequest setIncludesSubentities:NO];

// Declare sort descriptor variables
NSArray *requestSortDescriptors = nil;
NSSortDescriptor *sortDescriptorPrimary = nil;
NSSortDescriptor *sortDescriptorSecondary = nil;

// Set sort descriptors...
// Primary sort descriptor is your section array - sort by sections first.
sortDescriptorPrimary = [NSSortDescriptor sortDescriptorWithKey:self.sectionArray
ascending:YES];

// Secondary sort descriptor is your entity attribute - sort by fetched data second.
if (languageKey == 0) {
sortDescriptorSecondary = [NSSortDescriptor sortDescriptorWithKey:@"greekKey"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)];
// sectionTitleString = @"greekKey";

} else {
sortDescriptorSecondary = [NSSortDescriptor sortDescriptorWithKey:@"englishKey"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)];
// sectionTitleString = @"englishKey";

}

// Set sort descriptor array - section first, then table view data
requestSortDescriptors = @[sortDescriptorPrimary, sortDescriptorSecondary];

// Apply sort descriptors to fetch request
[fetchRequest setSortDescriptors:requestSortDescriptors];

// Your sectionNameKeyPath in your FRC is self.sectionArray (this may be incorrect - try)
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:self.sectionArray
cacheName:nil];

_fetchedResultsController.delegate = self;

return _fetchedResultsController;
}

关于iOS 索引 TableView 使用核心数据和排序 - 索引可以,但数据不行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25697374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com