ios - UITableView titleForHeaderInSection 未返回正确的 stringWithFormat

标签 ios objective-c iphone uitableview cocoa-touch

尝试在 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/

相关文章:

iphone - 如何打包组件以在多个 iPhone 应用程序中使用?

iphone - iPhone 应用程序中的 Settings.Bundle

iphone - 表格 View 不显示数据,iphone

iphone - TouchesBegan - 只需要最后一次触摸操作 - iPhone

ios - 前台本地通知

javascript - 在 ViewControllers 的 iOS 7 中使用 JavaScriptCore 从 javascript 触发 Objective-C 方法

ios - iOS 中的多路径 TCP

ios - 使用 CALayer 突出显示跨多行的 UITextView 中的文本

iphone - 哪些脚本语言解释器可以在 iOS 上运行?

iphone - 使用自动布局进行编辑时调整 TableView 中 subview 的大小