2012-07-31 65 views
0

在我的自定義表視圖單元格子類中,其中一個文本標籤的位置取決於ivar(NSString)的內容。 (即:如果NSString是空字符串,則文本標籤的框架位置不同)。UITableViewCell自定義子類視圖未更新

如果更新如下位置:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    customOverlayCell *myCell = [self.tableView dequeueReusableCellWithIdentifier:@"CustomOverlayCell"]; 

    if ([buildingFName isEqual:@""]) 
    { 
     CGRect titleLabelFrame = myCell.titleLabel.frame; 
     titleLabelFrame.origin.y = 45; 
     [myCell.titleLabel setFrame:titleLabelFrame]; 
    } 

    return myCell; 
} 

我已刪除那些不相關的部分代碼。

結果是出現在屏幕上的第一個單元格的佈局得到了正確更新,但滾動後出現的視圖佈局未更新。

我沒有正確使用dequeueReusableCellWithIdentifier嗎?或者是其他的錯誤?

編輯:從EJV

解決方案:

CGRect titleLabelFrame = myCell.titleLabel.frame; 

if ([buildingFName isEqual:@""]) 
{ 
    titleLabelFrame.origin.y = 45; 
} else { 
    titleLabelFrame.origin.y = 37; 
} 

[myCell.titleLabel setFrame:titleLabelFrame]; 

回答

1

如果標題標籤的框架是動態的,那麼當你出列從表視圖的單元格,框架可以在任一兩個狀態(當buildingFName爲空並且有字符時)。您需要確保您在buildingFName不爲空時設置框架。這樣,標題標籤的框架將始終正確設置。所以,你需要這樣的代碼:

CGRect titleLabelFrame = myCell.titleLabel.frame; 

if ([buildingFName isEqual:@""]) 
{ 
    titleLabelFrame.origin.y = 45; 
} else { 
    // Change titleLabelFrame 
} 

[myCell.titleLabel setFrame:titleLabelFrame]; 
+1

像魅力一樣工作。我的節目非常糟糕。謝謝。 – ratsimihah 2012-07-31 22:13:51

+0

@HeryR。沒問題。 – EJV 2012-07-31 22:24:14

1

我很害怕,它需要繼承的單元格,執行[UITableViewCell的layoutSubviews]適當佈置您的細胞子視圖。這是我如何做一個開關表視圖細胞類似的東西:

- (void)layoutSubviews 
{ 
    CGFloat const ESCFieldPadding = 10.0f; 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 

    // call super layout 
    [super layoutSubviews]; 

    // obtain widths of elements 
    CGFloat contentWidth = self.contentView.frame.size.width; 
    CGFloat contentHeight = self.contentView.frame.size.height; 
    CGFloat switchWidth = self.switchView.frame.size.width; 
    CGFloat switchHeight = self.switchView.frame.size.height; 
    CGFloat labelWidth = contentWidth - (4 * ESCFieldPadding) - switchWidth; 

    // correctly position both views 
    self.textLabel.frame = CGRectMake(ESCFieldPadding, 0.0f, 
             labelWidth, contentHeight); 
    // it is needed to explicitly resize font as for some strange reason, 
    // uikit will upsize the font after relayout 
    self.textLabel.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]]; 

    CGRect switchFrame = self.switchView.frame; 
    switchFrame.origin = CGPointMake(contentWidth - ESCFieldPadding - switchWidth, 
            (contentHeight/2) - (switchHeight/2)); 
    self.switchView.frame = CGRectIntegral(switchFrame); 

    [UIView commitAnimations]; 
} 
+0

我試圖把我的佈局修改中禁用自動佈局 - (空)layoutSubviews,但它並沒有改變任何東西。幸運的是,EJV找到了答案。感謝您的幫助! – ratsimihah 2012-07-31 22:13:00

0

嘗試從細胞

+0

謝謝,但是這個問題在一年前似乎已經解決了! – ratsimihah 2014-03-27 14:19:59

相關問題