【发布时间】:2011-05-30 13:35:05
【问题描述】:
我认为我需要按值而不是键排序....
这里是我填充数组的地方
const char *sql = "select cid, category from Categories ORDER BY category DESC";
sqlite3_stmt *statementTMP;
int error_code = sqlite3_prepare_v2(database, sql, -1, &statementTMP, NULL);
if(error_code == SQLITE_OK) {
while(sqlite3_step(statementTMP) == SQLITE_ROW)
{
int cid = sqlite3_column_int(statementTMP, 0);
NSString *category = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statementTMP, 1)];
NSArray *arr=[[NSArray alloc]initWithObjects:category,nil];
[arrayTmp setObject:arr forKey:[NSString stringWithFormat:@"%i",cid]];
[self.cidList addObject:[NSString stringWithFormat:@"%i",cid]];
[category release];
[arr release];
}
}
sqlite3_finalize(statementTMP);
sqlite3_close(database);
self.allCategories = arrayTmp;
[arrayTmp release];
这是对数组重新排序的方法。
- (void)resetSearch {
NSMutableDictionary *allCategoriesCopy = [self.allCategories mutableDeepCopy];
self.Categories = allCategoriesCopy;
[allCategoriesCopy release];
NSMutableArray *keyArray = [[NSMutableArray alloc] init];
[keyArray addObject:UITableViewIndexSearch];
[keyArray addObjectsFromArray:[[self.allCategories allKeys]
sortedArrayUsingSelector:@selector(compare:)]];
self.keys = keyArray;
[keyArray release];
}
这个问题我已经有一段时间了,上次我看这个我可以找到 sortedArrayUsingSelector 比较的替代方法?
编辑
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [Categories objectForKey:key];
static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SectionsTableIdentifier ];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: SectionsTableIdentifier ] autorelease];
}
cell.textLabel.text = [nameSection objectAtIndex:row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [Categories objectForKey:key];
NSLog(@"the selected cid is = %i",[key intValue]);
selectButton.enabled = YES;
}
有人吗?
【问题讨论】:
-
@Dave,它对我有用,我在使用 FMDB 时遇到问题,从未尝试过 CoreData
-
@Jules -- 您可能想详细说明您的问题是什么。您期望什么行为以及您看到的确切行为是什么。除非有明显的错误,否则很难从代码示例中准确推断出问题所在。
-
@TechZen 有了我的开场代码,屏幕上总是有一个表格视图、一个搜索栏和一个小键盘。加载时,tableview 会填充数据库中的所有类别。尽管这些是按字母顺序添加到主数组中的,但它们在 tableview 中显示。这是因为调用了 resetsearch,它改变了显示的内容。当用户在搜索栏中键入有限的结果时,这是由函数 handleSearchForTerm 完成的,该函数从用于显示结果的数组中删除项目。使用您提供的 resetSearch 代码,我在 tableview 中没有显示任何数据。
-
Cont... 我想在表格视图中按字母顺序查看结果,即使用户通过在搜索栏中输入/限制结果。
标签: iphone objective-c sorting nsmutablearray uitableview