2011-01-30 25 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