2011-01-30 49 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]; 

我想不顯示線如果內容(我從另一個變量得到的,讓我們在上面的例子中說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:函數期望傳遞實際對象,而不是指向對象的數組對象。

改爲撥打dataSourceWithArraysdataSourceWithItems。例如:

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

而且,你是從,@"Static Text"複製原來的例子實際上不是一排,這是一個部分的標題。所以在你的代碼中,你不會將這個字符串添加到myListOfRows。換句話說,靠近你的代碼的開頭,你應該刪除@"Static Text"行:

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

這些不同的方法來初始化TTSectionedDataSourceTTSectionedDataSource.h都記錄:

/** 
* 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 2011-02-02 14:04:19