2014-01-27 31 views
2

在該示例結束時我將子視圖添加到UIView,但是子目錄未顯示。 UIView顯示(使它成爲測試藍色,並且它在那裏)如果我單獨返回rowLabel或rowImage,它們顯示。但沒有嵌入到UIView中。有任何想法嗎?UIPickerView不顯示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; 
} 
+0

解決了。 「* rowView」被設置爲Y-height 160.這在某種程度上是很大的。當我把它降到80時,所有人都在正確的地方。現在我需要找出原因。它背後的規則是什麼? – iOSNoob

+0

顯示您的方法返回視圖的'UIView'可能* clip *您的視圖,請嘗試將'clipsToBounds'標誌設置爲'NO'。 - 它不會給你我假設的完整解決方案,但它可以幫助你調試問題。 –

+0

這真的是y高度。如果我把所有的Y高度... rowLabel,rowImage和rowView大小相同的60。所有工作正常。 clipsToBound也沒有工作。沒有效果。 – iOSNoob

回答

-1

insertSubview:atIndex:蘋果文檔:

子視圖屬性的數組中的索引在要插入 視圖。子視圖索引從0開始,不能大於子視圖數量的 。

換句話說:您還沒有任何子視圖,所以喲不能insert一個新的子視圖。 嘗試使用addSubview:代替:

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

不,不是問題所在。看到我的評論。它是* rowView Y-height的大小 – iOSNoob

0

你只需要有返回你的看法,你不需要做[rowView insertSubview:rowImage atIndex:0]。這是更好地做這樣

(如果組分== 0) [rowView addsubview:rowImage]

(如果組分== 1) [rowView addsubview:rowLabel]

返回rowView

相關問題