TTTableViewController fill in dataSource with array of objects variable
I have used the example of TTTableViewController to display an array that can have a variable number of rows / images / text. With the example given, the list of objects is hard-coded during initialization if the ViewController, for instance:
self.dataSource = [TTSectionedDataSource dataSourceWithObjects:
@"Static Text",
[TTTableTextItem itemWithText:@"TTTableItem"],
[TTTableCaptionItem itemWithText:@"TTTableCaptionItem" caption:@"Text"],
[TTTableSubtextItem itemWithText:@"TTTableSubtextItem" caption:kLoremIpsum],
nil];
I want not to display a line if the content (that i get from another variable, let's say kLoremIpsum
in the example above) is empty. To do so, I have tried:
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];
But it does not work, my TTTableView remains completely empty. I can see that the table is properly working with the number of objects I expect开发者_开发问答. Why this code does not work?
At the end, where you call [TTSectionedDataSource dataSourceWithObjects:]
, you pass it myListOfRows
, which is an array; but the dataSourceWithObjects:
function expects to be passed the actual objects, not an array object that points to the objects.
Call dataSourceWithArrays
or dataSourceWithItems
instead. For example:
self.dataSource = [TTSectionedDataSource dataSourceWithArrays:@"Static Text",
myListOfRows, nil];
Also, in the original example that you are copying from, @"Static Text"
is not actually a row, it is a section title. So in your code, you would not add this string to myListOfRows
. In other words, near the beginning of your code, you should remove the @"Static Text"
line:
myListOfRows = [NSMutableArray arrayWithObjects:
// @"Static Text", // <-- commented out this line!
[TTTableTextItem itemWithText:@"TTTableItem"],
[TTTableCaptionItem itemWithText:@"TTTableCaptionItem" caption:@"Text"],
nil];
These different ways to initialize a TTSectionedDataSource
are documented in TTSectionedDataSource.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;
精彩评论