我有一个UITableView
,其中自定义cellView
填充了一个数组(cf. functs),没有任何部分。我的目标是创建一个带有列表索引的搜索栏(这是可以的)(这是问题)。
在阅读了大量文档后,我计划使用这三种方法:“numberOfSectionsInTableView
”,“sectionIndexTitlesForTableView
”和“sectionForSectionIndexTitle
”
如何使用以下结构创建NSDictionnary
个functs数组?
//in viewDidLoad
// Initialize the functs array
Funct *funct1 = [Funct new];
funct1.name = @"AAA";
funct1.detail = @"detail1...";
funct1.image = @"a.jpg";
Funct *funct2 = [Funct new];
funct2.name = @"BBB";
funct2.prepTime = @"detail2...";
funct2.image = @"b.jpg";
functs = [NSArray arrayWithObjects:funct, funct2, nil]; //etc.
//For Index list: one section per letter
NSString *letters = @"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";
self.indexTitlesArray = [letters componentsSeparatedByString:@" "];
提前致谢
答案 0 :(得分:0)
Here你有一个很好的示例项目教程。这就是你要找的东西。
这是TableView索引的一个示例,但它有一个很好的例子,用于分段和行
在此示例中,此方法对您非常有用
static NSString *letters = @"abcdefghijklmnopqrstuvwxyz";
// This method returns an array of dictionaries where each key is a letter
// and each value is a group of words corresponding to the letter.
+ (NSArray *) wordsFromLetters {
NSMutableArray *content = [NSMutableArray new];
for (int i = 0; i < [letters length]; i++ ) {
NSMutableDictionary *row = [[[NSMutableDictionary alloc] init] autorelease];
char currentWord[WORD_LENGTH + 1];
NSMutableArray *words = [[[NSMutableArray alloc] init] autorelease];
for (int j = 0; j < WORD_LENGTH; j++ ) {
if (j == 0) {
currentWord[j] = toupper([letters characterAtIndex:i]);
}
else {
currentWord[j] = [letters characterAtIndex:i];
}
currentWord[j+1] = '\0';
[words addObject:[NSString stringWithCString:currentWord encoding:NSASCIIStringEncoding]];
}
char currentLetter[2] = { toupper([letters characterAtIndex:i]), '\0'};
[row setValue:[NSString stringWithCString:currentLetter encoding:NSASCIIStringEncoding]
forKey:@"headerTitle"];
[row setValue:words forKey:@"rowValues"];
[content addObject:row];
}
return content;
}
您可以从此资源中获取主要想法,但如果您愿意,我会调整代码。我认为工作正常
NSMutableArray *funcsArray = [NSMutableArray array];//Here you add all your functs objects
[funcsArray addObjects: funct1, funct2...];
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"name"
ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray;
sortedArray = [funcsArray sortedArrayUsingDescriptors:sortDescriptors];
NSMutableArray *funcsArrayOrdered = [NSMutableArray array];//Here you add all your functs objects
NSMutableArray *funcsIndexed = [NSMutableArray array];
NSMutableArray *letters = [NSMutableArray array];
NSString *currentLetter = nil;
int numItems = [funcsArrayOrdered count];
for (Funct *functObj in funcsArrayOrdered) {
NSLog(@"funct: %@", functObj.name);
numItems--;
NSString *string = userT.name;
if (string.length > 0) {
NSString *letter = [[string substringToIndex:1] uppercaseString];
if ([currentLetter length] == 0) {
currentLetter = letter;
}
if (![letter isEqualToString:currentLetter] || numItems == 0) {
if ([letter isEqualToString:currentLetter] && numItems == 0) {
[letters addObject:functObj];
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setValue:currentLetter forKey:@"headerTitle"];
[dic setValue:letters forKey:@"rowValues"];
[funcsIndexed addObject:dic];
letters = [NSMutableArray array];
}else{
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setValue:currentLetter forKey:@"headerTitle"];
[dic setValue:letters forKey:@"rowValues"];
[funcsIndexed addObject:dic];
letters = [NSMutableArray array];
[letters addObject:functObj];
currentLetter = letter;
if (numItems == 0 && [funcsArrayOrdered count] > 1) {
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setValue:currentLetter forKey:@"headerTitle"];
[dic setValue:letters forKey:@"rowValues"];
[funcsIndexed addObject:dic];
}
}
}else {
[letters addObject:functObj];
}
}
}
现在,您将在funcsIndexed
中排序您的词典数组