2014-05-15 32 views
2

我有一個UIView一個UITableViewCell內(動態原型,不知道這是否澄清這一點很重要)與background color,並用下面的代碼改變了view's frame(內cellForRowAtIndexPath法) :的UIView調整內部一個UITableViewCell不顯示,除非表滾動

UIView* verde = (UIView*) [cell viewWithTag:202]; 
verde.frame =CGRectMake(20, 30, x, y); 

的問題是,當UITableView繪製的第一次(在屏幕加載時)的UIView具有原始大小(默認爲原始樣機從情節串連圖板建立)。但是當我向下滾動時,單元離開了屏幕,因此被另一個單元重新使用。當滾動回到單元格時,cellForRowAtIndexPath被再次調用,現在該框架具有正確的大小。

我試過在沒有成功的情況下更改幀後嘗試調用[verde setNeedsDisplay];

+0

您是否在使用自動佈局? –

+0

你可以把'cellForRowAtIndexPath'代碼? – iphonic

+0

禁用自動佈局解決了這個問題,謝謝! – Fdo

回答

2

禁用自動版式解決問題作爲蒂莫西·穆斯指出。不知何故,第一次繪製中的單元格(屏幕第一次加載)保留故事板中指定的佈局,當它們離開屏幕並且它們被重新使用或再次創建時,視圖最終以正確的框架繪製。

+0

是否有另一種解決方案不需要禁用自動佈局? – Oren

+0

你如何以編程方式禁用自動佈局? – Unome

+0

禁用自動佈局解決了我的問題(由UIView首次創建時自動佈局干涉導致),可能在創建視圖後操縱框架也可以解決問題,但禁用自動佈局完成作業@Oren – Fdo

0

試試這個,希望你能解決這個問題

-(UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    else{ 
     [[cell.contentView viewWithTag: 202] removeFromSuperview]; 
    } 

    UIView *view =[[UIView alloc]init]; 
    view.frame = cell.contentView.frame;   
    view.tag = 202; 

    view.backgroundColor = [UIColor redColor];//To be sure that the custom view in the cell 
    [cell addSubview:view]; 

    return cell; 

} 
+0

感謝您的調試想法,這是一個很好的。禁用Autolayout解決了這個問題。 – Fdo

相關問題