2011-01-30 5 views
0

TTTableViewControllerの例を使用して、行数/画像/テキストの可変数を持つ配列を表示しました。TTTableViewControllerオブジェクトの配列でdataSourceを設定する変数

self.dataSource = [TTSectionedDataSource dataSourceWithObjects: 
    @"Static Text", 
    [TTTableTextItem itemWithText:@"TTTableItem"], 
    [TTTableCaptionItem itemWithText:@"TTTableCaptionItem" caption:@"Text"], 
    [TTTableSubtextItem itemWithText:@"TTTableSubtextItem" caption:kLoremIpsum], 
    nil]; 

は私が別の変数から取得することを内容は(、のを許可すればに行を表示しないをしたい:与えられた例では、オブジェクトのリストは、例えば、ハードコードされたのViewController場合、初期化中です上記の例ではkLoremIpsum)が空です。これを行うには、私は試しました:

NSMutableArray * myListOfRows; 
myListOfRows = [NSMutableArray arrayWithObjects: 
    @"Static Text", 
    [TTTableTextItem itemWithText:@"TTTableItem"], 
    [TTTableCaptionItem itemWithText:@"TTTableCaptionItem" caption:@"Text"], 
    nil]; 

if(kLoremIpsum != nil) { 
    [myListOfRows addObject:[TTTableSubtextItem 
          itemWithText:@"TTTableSubtextItem" 
            caption:kLoremIpsum]]; 
} 

self.dataSource = [TTSectionedDataSource dataSourceWithObjects: 
    myListOfRows, 
    nil]; 

しかし、私のTTTableViewは完全に空のままです。私は、テーブルが私が期待するオブジェクトの数で適切に動作していることがわかります。なぜこのコードが機能しないのですか?

答えて

2

最後に[TTSectionedDataSource dataSourceWithObjects:]と呼ぶ場所には、myListOfRowsという配列が渡されます。 dataSourceWithObjects:関数は、オブジェクトを指す配列オブジェクトではなく、実際のオブジェクトを渡すことを想定しています。

代わりにdataSourceWithArraysまたはdataSourceWithItemsと電話してください。たとえば:

self.dataSource = [TTSectionedDataSource dataSourceWithArrays:@"Static Text", 
        myListOfRows, nil]; 

はまた、あなたが@"Static Text"からコピーされ、元の例では、セクションタイトルで、実際の行ではありません。だからあなたのコードでは、myListOfRowsにこの文字列を追加しません。 TTSectionedDataSourceを初期化する

myListOfRows = [NSMutableArray arrayWithObjects: 
    // @"Static Text", // <-- commented out this line! 
    [TTTableTextItem itemWithText:@"TTTableItem"], 
    [TTTableCaptionItem itemWithText:@"TTTableCaptionItem" caption:@"Text"], 
    nil]; 

これらのさまざまな方法がTTSectionedDataSource.hに記載されています:つまり、あなたのコードの先頭付近に、あなたは@"Static Text"行削除する必要があります明らかだ

/** 
* Objects should be in this format: 
* 
* @"section title", item, item, @"section title", item, item, ... 
* 
* Where item is generally a type of TTTableItem. 
*/ 
+ (TTSectionedDataSource*)dataSourceWithObjects:(id)object,...; 

/** 
* Objects should be in this format: 
* 
* @"section title", arrayOfItems, @"section title", arrayOfItems, ... 
* 
* Where arrayOfItems is generally an array of items of type TTTableItem. 
*/ 
+ (TTSectionedDataSource*)dataSourceWithArrays:(id)object,...; 

/** 
* @param items 
* 
* An array of arrays, where each array is the contents of a 
* section, to be listed under the section title held in the 
* corresponding index of the `section` array. 
* 
* @param sections 
* 
* An array of strings, where each string is the title 
* of a section. 
* 
* The items and sections arrays should be of equal length. 
*/ 
+ (TTSectionedDataSource*)dataSourceWithItems:(NSArray*)items sections:(NSArray*)sections; 
+0

を、最終的には私のようにする必要がありdataSourceWithItemsを使用して、2つの配列で必要な処理を行います。あなたの答えの質をありがとう! – ceyquem