2013-07-28 37 views
0

我在我的tableview中的每個單元格內有一個滾動視圖,雖然我的scrollview中嵌入的圖片不顯示,直到我滾動tableview上下... 我有一個自定義類與@滾動視圖和單元格的屬性。我有我的代碼設置cellForRowAtIndexPath:方法中的滾動視圖。這應該在最初創建單元時調用?我很困惑。在UITableView內容問題的UIScrollView

當我啓動應用程序時,如何擺脫該問題並使圖像首先出現?

相關代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellID = @"CustomID"; 

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; 

    if (cell == nil) { 
     cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; 
    } 

    for (int i = 0; i < [imageArray count]; i++) { 
     CGRect frame; 
     frame.origin.x = cell.scrollView.frame.size.width * i; 
     frame.origin.y = 0; 
     frame.size = cell.scrollView.frame.size; 

     UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame]; 
     imageView.image = [UIImage imageNamed:[imageArray objectAtIndex:i]]; 
     [cell.scrollView addSubview:imageView]; 
    } 

    cell.scrollView.contentSize = CGSizeMake(cell.scrollView.frame.size.width * [imageArray count], cell.scrollView.frame.size.height); 

    return cell; 
} 
+0

對不起,你的問題是什麼? –

+0

你在哪裏實例化滾動視圖?您是否在IB/Storyboard Editor中設計了「CustomCell」? –

+0

單元格和滾動視圖的默認大小是什麼 - 是等於「CGRectZero」的框架? – Wain

回答

1

這不完全是關係到你的問題,但你會遇到這個問題太:

不要忘記,你的細胞會被重用。在回用(可以說用戶向下滾動,細胞向上移動到屏幕和底部出現下一個將是物理,只是disappeard和重複使用的CustomCell的實例。)

因此增加:

for (UIView *aView in [NSArray arrayWithArray:cell.subviews]) { 
     [aView removeFromSuperview]; 
     // if you don't arc then release them and take care of their subviews - if any. 
    } 

在添加任何新的UIImageView之前。 (我以爲有一種方法可以一次去除所有子視圖但沒有找到它)

+0

在這種情況下,我認爲他應該從'cell.scrollView.subviews'中刪除子視圖。請注意,如果您從UIScrollView中刪除所有子視圖,則滾動指示器也將被刪除。您需要進行某種檢查(基於標記或課程),以便您只能從滾動視圖中刪除您的內容。 –