2012-02-10 22 views
1

我使用以下代碼創建的自定義的UITableViewCell:自定義的UITableViewCell未正常顯示時的UITableViewController第一負載

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 

     mapButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];  
     [[self contentView] addSubview:mapButton];   

     houseNumberLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
     [[self contentView] addSubview:houseNumberLabel]; 
     [houseNumberLabel release]; 

     NSArray *segmentArray = [[NSArray alloc] initWithObjects:@"Yes", @"No", nil]; 
     segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentArray]; 
     [segmentArray release]; 
     [segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar]; 
     [[self contentView] addSubview:segmentedControl]; 
     [segmentedControl release]; 

     disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     [[self contentView] addSubview:disclosureButton];    
    } 

    return self; 
} 

我已經然後重寫的layoutSubviews方法用下面的代碼的自定義單元格:

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 

    CGRect viewBounds = [[self contentView] bounds]; 
    float h = viewBounds.size.height; 
    float w = viewBounds.size.width; 

    float inset = 20.0; 
    float mapButtonWidth = mapButton.frame.size.width; 
    float segmentWidth = 140.0; 
    float disclosureButtonWidth = disclosureButton.frame.size.width; 
    float houseNumberLabelWidth = viewBounds.size.width - (mapButtonWidth + inset + inset + segmentWidth + inset + disclosureButtonWidth + inset); 

    CGRect frame = CGRectMake(0, 0, h, mapButtonWidth); 
    [mapButton setFrame:frame]; 

    frame.origin.x += mapButtonWidth + inset; 
    frame.size.width = houseNumberLabelWidth; 
    [houseNumberLabel setFrame:frame]; 

    frame.origin.x += houseNumberLabelWidth + inset; 
    frame.size.width = segmentWidth; 
    [segmentedControl setFrame:frame]; 

    frame.origin.x += segmentWidth + inset; 
    frame.size.width = disclosureButtonWidth; 
    [disclosureButton setFrame:frame]; 
} 

我已經將日誌添加到這些函數中,並且每次創建單元時都會調用它們。問題是當UITableViewController第一次加載並顯示單元格顯示的單元格時,就好像layoutSubviews從未被調用(即使根據它所具有的日誌)。但是,當我滾動瀏覽表格視圖時,新的單元格(屏幕外)正確顯示。然後,我向後滾動,現在第一個單元格正確。就好像layoutSubviews被調用一樣,但是當表視圖第一次被加載時,它沒有意識到它需要使用新的佈局進行渲染。

我已經添加了一個reloadData viewWillAppear部分修復了這個問題,但最後一個單元格(剛剛關閉屏幕)仍呈現不正確的方式。我正在拉我的頭髮。

[筆記本都]我只是增加了一個日誌:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 

這是運行的8倍(一個用於每個小區的第一被顯示爲表視圖)BEFORE layoutSubviews被調用。 layoutSubviews在此之後被調用(因此爲什麼格式不正確)。我應該在這裏添加一個[cell layoutSubviews](它確實有效,但之後會再次調用)還是有其他方法?

+0

那麼,你在做什麼'willDisplayCell'這是造成這種情況?我建議在cellForRowAtIndexPath中設置單元格的大小,或者在willDisplayCell單元格中調用的任何方法應該調用setNeedLayout,如果改變數據意味着它需要佈局。 – mattjgalloway 2012-02-11 11:03:16

回答

0

我解決了這個問題(我對這個錯誤有點尷尬),但是最初我錯誤地設置了CGRect尺寸。我正在爲高度放寬度和寬度。代碼應該是:

CGRect frame = CGRectMake(0, 0, mapButtonWidth, h); 

一個很容易犯的錯誤,但產生不尋常的結果。

相關問題