2012-02-24 13 views
2

tableViewのtitleForHeaderInセクションで複合文字列を返そうとすると、奇妙な問題が発生します。UITableView titleForHeaderInSectionが正しいstringWithFormatを返しません

文字列をNSLogすれば良いと思われますが、返すとクラッシュします!

はここに私のコードです: "5" による

- (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... 
    } 

私が交換した場合、 "[0] [0](* _tabsections_especes)"、例えば、問題が解消されません。 したがって、問題はstringWithFormatメソッドでNSLocalizedStringを使用し、それを返すことです。

私は間違っていますか?

+0

あなたの戻り値のようなサウンドは、テーブルビューでアクセスできるようになる前にリリースされています。 NSLocalizedStringを使わずにタイトルを直接設定しようとしましたか? –

+0

はい、実際は悪化しています! 'return @ NSString stringWithFormat:@"%@(%d) "、@" titre "、5];'を書くと、アプリがクラッシュします。私は本当に何が起こっているのか分からないのですか?このコードはまだ非常に簡単ですか? – Chrysotribax

+0

OK、キースおよびフィブノキー。 – Chrysotribax

答えて

0

私は問題がどこにあるかを発見しました。 実際にはtableView:titleForHeaderInSectionではなく、tableView:viewForHeaderInSectionです。

実際、viewForHeaderInSectionにUIViewのサブクラスを使用しているからです。 このサブクラスでは、私は "title"という名前のivarを持っています。このサブクラスのinitメソッドで

は、私はこのように、このIVARを設定します。

title = myTitle; // (myTitle is an argument of the custom init method) 

そして、ちょうど後に、私はのdrawRectメソッドでは、このようなこのタイトルを使用します。

[title drawAtPoint:CGPointMake(8, 9) withFont:[UIFont systemFontOfSize:19]]; 

このtitleForHeaderInSectionの@ "example string"のような静的な文字列を渡し、viewForHEaderInSection経由でうまく動作します。

stringWithFormatのような自動解放オブジェクトを渡しても、まったくそうではありません。それは、このよう

[title dealloc]; 

title = [myTitle retain]; 

と私のサブクラスのdeallocメソッドでそれを解放するために:

ので、解決策は、このようなサブクラスで私のIVARを保持するだけです動作し、クラッシュしません。私はこれが役立ち、その説明がはっきりしていることを願っています。

2

私はあなたの方法をテストし、それが動作

NSString *result = [NSString stringWithFormat:@"%@ : %d)", title, number ]; 
return result; 

またはこの

NSString *result = [[NSString alloc]initStringWithFormat:@"%@ : %d)", title, number ]]autorelease]; 
return result; 
+0

これはOPと同じではありませんか? –

0

を使用する前にこれを使用してください。他の場所であなたのバグを探してください。

+0

あなたは正しいです、私の自動応答を見てください!ありがとう! – Chrysotribax

関連する問題