开发者

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.

  1. UILabel *label = (UILabel *)cell; will create a pointer to exactly the same item cell and overwrite whatever is there.
  2. If dictionary is nil then [dictionary objectForKey:@"ArrayKey"] must cause a crash.
  3. id key should be NSString *key in order to assign it to label.text.
  4. You are not adding the label anywhere as a subview.

You need at least a [cell.contentView addSubView:label];.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜