jjzjj

ios - UITableView titleForHeaderInSection 不返回正确的 stringWithFormat

coder 2024-01-21 原文

尝试在 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...
    } 

例如,如果我用“5”替换“(*_tabsections_especes)[0][0]”,问题仍然存在。 所以,问题似乎是关于在 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/

有关ios - UITableView titleForHeaderInSection 不返回正确的 stringWithFormat的更多相关文章

随机推荐