Reading Array within NSDictionary into UITableViewCell Subviews
I am attempting to read the plist dictionary below into cells of a UITableView. I want to create a subview for each of the strings contained in the array denoted by the ArrayKey like:
Row1: | myString1 |
Row2: | myString2 || myString3 |
I've used fast enumeration to read the dictionary, but I can only successfully read the first string in each array:
CGFloat constant = 0;
for (NSArray key in [dictionary objectForKey:@"ArrayKey"]) {
UILabel *label = (UILabel *)cell;
label.text = (NSString *)key;
label.frame = CGRectMake(0 + constant, 0, 30, 30);
constant = 20 + CGRectGetMaxX(label.frame);
}
I'm missing the subview piece. And I'm puzzled why if I add an NSLog of the fast enumeration, the output will show all strings in the array, but I can only display the first (ie myString1 in one row and myString2 in a second row without myString3).
<dict>
<key>TitleKey</key>
<string>myTitle1</string>
<key>ArrayKey</key>
<array>
<string>myString1</string>
</array>
</dict>
<dict>
<key>TitleKey</k开发者_如何学Goey>
<string>myTitle2</string>
<key>ArrayKey</key>
<array>
<string>myString2</string>
<string>myString3</string>
</array>
</dict>
Mystifying why this code is working at all. I am sure you are posting only partial code.
UILabel *label = (UILabel *)cell;
will create a pointer to exactly the same itemcell
and overwrite whatever is there.- If
dictionary
isnil
then[dictionary objectForKey:@"ArrayKey"]
must cause a crash. id key
should beNSString *key
in order to assign it tolabel.text
.- You are not adding the label anywhere as a subview.
You need at least a [cell.contentView addSubView:label];
.
精彩评论