尝试在 tableView 的 titleForHeaderIn 部分返回复合字符串时遇到一个奇怪的问题。
如果我 NSLog 字符串,它似乎很好,但是当我返回它时,它崩溃了!
这是我的代码:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSString *title = NSLocalizedString(@"favorites",@"");
NSLog(@"%@", title); // this prints the correct title ("Items" for example...)
int number = (*_tabsections_especes)[0][0];
NSLog(@"%d", number); // this prints the correct number ( "5", for example...)
NSLog(@"%@", [NSString stringWithFormat:@"%@ : %d", title, number ] );
// this prints the correct concatenated string ("Items : 5", for example);
return [NSString stringWithFormat:@"%@ : %d)", title, number ];
// --> this either crashes the app, or returns anything in the title,
// for example the title of a resource image or another pointer...
}
例如,如果我将“(*_tabsections_especes)[0][0]”替换为“5”,问题仍然存在。 因此,问题似乎在于在 stringWithFormat 方法中使用 NSLocalizedString,然后返回它。
我做错了什么?
最佳答案
之前使用过这个
NSString *result = [NSString stringWithFormat:@"%@ : %d)", title, number ];
return result;
或者使用这个
NSString *result = [[NSString alloc]initStringWithFormat:@"%@ : %d)", title, number ]]autorelease];
return result;
关于ios - UITableView titleForHeaderInSection 未返回正确的 stringWithFormat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9430799/