- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
if(tempArray != nil){
for (int i = 0; i < [tempArray count]; i++)
{
if([[sectionInfo indexTitle] isEqualToString:[tempArray objectAtIndex:i]])
// if([sectionInfo indexTitle] == [tempArray objectAtIndex:i])
{
NSLog(@"found");
break;
} else
{
NSLog(@"Not found %@", [sectionInfo indexTitle]);
[tempArray addObject:[sectionInfo indexTitle]];
NSLog(@"array %@", tempArray);
return [tempArray objectAtIndex:i];
}
}
}
}
if语句中字符串的比较永远不会解析为true。为了测试目的,示例数据有两个重复的实例。注释行是一个替代行,尽管我认为不正确,但尝试将节与temparray中的字符串进行比较。
我做错什么了?
而且,所有数据都是大写字母,所以比较不是大小写的问题。
编辑:
NSLogs:
NSLog(@"Not found %@", [sectionInfo indexTitle]);
NSLog(@"section: %d", section);
NSLog(@"[sectionInfo indexTitle] %@", [sectionInfo indexTitle]);
NSLog(@"[sectionInfo objects] %@", [[sectionInfo objects] valueForKey:@"name"]);
NSLog(@"array %@", tempArray);
给我这些结果:
2010-06-08 11:25:03.276 XYZApplication[5287:207] Not found Z
2010-06-08 11:25:03.277 XYZApplication[5287:207] section: 10
2010-06-08 11:25:03.279 XYZApplication[5287:207] [sectionInfo indexTitle] Z
2010-06-08 11:25:03.284 XYZApplication[5287:207] [sectionInfo objects] (
Z
)
2010-06-08 11:25:03.284 XYZApplication[5287:207] array (
""
)
2010-06-08 11:25:03.285 XYZApplication[5287:207] Not found A
2010-06-08 11:25:03.285 XYZApplication[5287:207] section: 0
2010-06-08 11:25:03.286 XYZApplication[5287:207] [sectionInfo indexTitle] A
2010-06-08 11:25:03.286 XYZApplication[5287:207] [sectionInfo objects] (
"Andy the Clown"
)
2010-06-08 11:25:03.287 XYZApplication[5287:207] array (
"",
Z
)
2010-06-08 11:25:03.288 XYZApplication[5287:207] Not found B
2010-06-08 11:25:03.289 XYZApplication[5287:207] section: 1
2010-06-08 11:25:03.289 XYZApplication[5287:207] [sectionInfo indexTitle] B
2010-06-08 11:25:03.290 XYZApplication[5287:207] [sectionInfo objects] (
"Baseball Annie"
)
2010-06-08 11:25:03.291 XYZApplication[5287:207] array (
"",
Z,
A
)
2010-06-08 11:25:03.291 XYZApplication[5287:207] Not found B
2010-06-08 11:25:03.292 XYZApplication[5287:207] section: 2
2010-06-08 11:25:03.292 XYZApplication[5287:207] [sectionInfo indexTitle] B
2010-06-08 11:25:03.293 XYZApplication[5287:207] [sectionInfo objects] (
"Baseball Bugs"
)
2010-06-08 11:25:03.293 XYZApplication[5287:207] array (
"",
Z,
A,
B
)
2010-06-08 11:25:03.294 XYZApplication[5287:207] Not found D
2010-06-08 11:25:03.295 XYZApplication[5287:207] section: 3
2010-06-08 11:25:03.295 XYZApplication[5287:207] [sectionInfo indexTitle] D
2010-06-08 11:25:03.296 XYZApplication[5287:207] [sectionInfo objects] (
""
)
2010-06-08 11:25:03.296 XYZApplication[5287:207] array (
"",
Z,
A,
B,
B
)
2010-06-08 11:25:03.296 XYZApplication[5287:207] Not found E
2010-06-08 11:25:03.304 XYZApplication[5287:207] section: 4
2010-06-08 11:25:03.305 XYZApplication[5287:207] [sectionInfo indexTitle] E
2010-06-08 11:25:03.306 XYZApplication[5287:207] [sectionInfo objects] (
E
)
2010-06-08 11:25:03.306 XYZApplication[5287:207] array (
"",
Z,
A,
B,
B,
D
)
2010-06-08 11:25:03.307 XYZApplication[5287:207] Not found F
2010-06-08 11:25:03.307 XYZApplication[5287:207] section: 5
2010-06-08 11:25:03.308 XYZApplication[5287:207] [sectionInfo indexTitle] F
2010-06-08 11:25:03.310 XYZApplication[5287:207] [sectionInfo objects] (
Frank
)
2010-06-08 11:25:03.310 XYZApplication[5287:207] array (
"",
Z,
A,
B,
B,
D,
E
)
2010-06-08 11:25:03.311 XYZApplication[5287:207] Not found H
2010-06-08 11:25:03.311 XYZApplication[5287:207] section: 6
2010-06-08 11:25:03.312 XYZApplication[5287:207] [sectionInfo indexTitle] H
2010-06-08 11:25:03.313 XYZApplication[5287:207] [sectionInfo objects] (
H
)
2010-06-08 11:25:03.313 XYZApplication[5287:207] array (
"",
Z,
A,
B,
B,
D,
E,
F
)
2010-06-08 11:25:03.314 XYZApplication[5287:207] Not found J
2010-06-08 11:25:03.315 XYZApplication[5287:207] section: 7
2010-06-08 11:25:03.315 XYZApplication[5287:207] [sectionInfo indexTitle] J
2010-06-08 11:25:03.316 XYZApplication[5287:207] [sectionInfo objects] (
J
)
2010-06-08 11:25:03.316 XYZApplication[5287:207] array (
"",
Z,
A,
B,
B,
D,
E,
F,
H
)
2010-06-08 11:25:03.317 XYZApplication[5287:207] Not found J
2010-06-08 11:25:03.317 XYZApplication[5287:207] section: 8
2010-06-08 11:25:03.329 XYZApplication[5287:207] [sectionInfo indexTitle] J
2010-06-08 11:25:03.330 XYZApplication[5287:207] [sectionInfo objects] (
Joe
)
2010-06-08 11:25:03.330 XYZApplication[5287:207] array (
"",
Z,
A,
B,
B,
D,
E,
F,
H,
J
)
2010-06-08 11:25:03.335 XYZApplication[5287:207] Not found L
2010-06-08 11:25:03.336 XYZApplication[5287:207] section: 9
2010-06-08 11:25:03.336 XYZApplication[5287:207] [sectionInfo indexTitle] L
2010-06-08 11:25:03.337 XYZApplication[5287:207] [sectionInfo objects] (
L
)
2010-06-08 11:25:03.337 XYZApplication[5287:207] array (
"",
Z,
A,
B,
B,
D,
E,
F,
H,
J,
J
)