UIPickerView没有在UIView中显示添加的子视图

时间:2014-01-27 14:03:41

标签: ios iphone objective-c uiview uipickerview

在该示例的末尾我将子视图添加到UIView,但是没有显示。 UIView正在显示(用于测试蓝色并且它在那里)如果我分别返回rowLabel或rowImage,它们正在显示。但没有嵌入UIView。任何想法?

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {

    NSDictionary *oneResultDict = [_postArray objectAtIndex:row];

    NSData *tmpImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[oneResultDict objectForKey:@"thumbnail"]]];

    UIImage *tmpImage = [UIImage imageWithData:tmpImageData];

    UIImageView *rowImage = [[UIImageView alloc] initWithImage:tmpImage];
    rowImage.frame = CGRectMake(0, 0, 60, 60);

    UILabel *rowLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 0, 200, 60)];
    rowLabel.text = [oneResultDict objectForKey:@"title"];

    UIView *rowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 160)];

    [rowView insertSubview:rowImage atIndex:0];
    [rowView insertSubview:rowLabel atIndex:1];

    return rowView;
}

2 个答案:

答案 0 :(得分:0)

你只需要返回你的视图,你不需要[rowView insertSubview:rowImage atIndex:0]。最好像

一样

(如果组件== 0)      [rowView addsubview:rowImage]

(如果组件== 1)      [rowView addsubview:rowLabel]

返回rowView

答案 1 :(得分:-1)

来自insertSubview:atIndex: Apple文档:

  

要插入的子视图属性数组中的索引   视图。子视图索引从0开始,不能大于   子视图的数量。

换句话说:您还没有任何子视图,因此您不能insert新的子视图。 请尝试使用addSubview:

[rowView addSubview:rowImage];
[rowView addSubview:rowLabel];