2011-06-07 31 views
14

我使用cell.contentView.bounds.size.width來計算在一個UITableView細胞中的文本字段的位置。當單元格被創建時,調試代碼將寬度報告爲302.當單元格滾動屏幕然後再次打開時,調試代碼每次報告它是280。它似乎不想回到302並停留在280.最終的結果是,第二次將字段放入單元格的contentView時,文本字段被放置在錯誤的位置,儘管它放在了第一次正確的地方。的UITableView cell.contentView.bounds.size.width改變與小區重用

我圖22是顯著莫名其妙,但我不知道它是什麼。猜測它可能是披露箭頭,我在寬度測定的前面移動了「清除單元」代碼,包括將配件設置爲nada。

有人可以告訴我這裏發生了什麼事嗎?

( - 這是我所知道的 - 與不相關的東西剪掉了)的代碼如下所示:

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    NSUInteger section = [indexPath section]; 
    NSUInteger row = [indexPath row]; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 


    // Configure the cell. 

    while([cell.contentView.subviews count]){ 
     id subview = [cell.contentView.subviews objectAtIndex:0]; 
     [subview removeFromSuperview]; 
    } 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

8< snip! 

    CGFloat theCellWidth = cell.contentView.bounds.size.width - 44.0; 
    CGFloat theLineHeight = [[UIFont boldSystemFontOfSize: [UIFont labelFontSize]+1.0] lineHeight]; 

    NSLog(@"cell.contentView.bounds.size.width %1.0f",cell.contentView.bounds.size.width); 

    if (0==section) { 
     switch (row) { 
      case 2: 
       while([cell.contentView.subviews count]){ 
        id subview = [cell.contentView.subviews objectAtIndex:0]; 
        [subview removeFromSuperview]; 
       } 
       cell.selectionStyle = UITableViewCellSelectionStyleNone; 
       cell.textLabel.text = @" "; 
       cell.detailTextLabel.text = @"The Age"; 
       theAgeTextField.frame = CGRectMake(10.0, 2.0, theCellWidth, theLineHeight); 
//    NSLog(@"cell.contentView %@",cell.contentView); 
       theAgeTextField.text = theAge; 
       theAgeTextField.font = [UIFont boldSystemFontOfSize: [UIFont labelFontSize]+1.0]; 
       theAgeTextField.keyboardType = UIKeyboardTypeDecimalPad; 
       theAgeTextField.borderStyle = UITextBorderStyleNone; 
       theAgeTextField.userInteractionEnabled = NO; 
       [cell.contentView addSubview:theAgeTextField]; 
       cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
       break; 

8< snip! (lots of closing braces and other stuff omitted) 

    return cell; 
} 

想試試這一個人在家,男孩和女孩?

開始使用新的基於導航的應用。將下面的代碼放到RootViewController.m:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 5; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    NSLog(@"cell.contentView.bounds.size.width %1.0f",cell.contentView.bounds.size.width); 
    // Configure the cell. 
    return cell; 
} 

只有兩個來實現這一目標所需的默認代碼變化:改變部分的行數(「返回5」)和的風格cell必須是UITableViewCellStyleSubtitle。然後,當你運行程序,你會看到這五線:

2011-06-18 11:10:19.976 TestTableCells[7569:207] cell.contentView.bounds.size.width 302 
2011-06-18 11:10:19.978 TestTableCells[7569:207] cell.contentView.bounds.size.width 302 
2011-06-18 11:10:19.979 TestTableCells[7569:207] cell.contentView.bounds.size.width 302 
2011-06-18 11:10:19.980 TestTableCells[7569:207] cell.contentView.bounds.size.width 302 
2011-06-18 11:10:19.982 TestTableCells[7569:207] cell.contentView.bounds.size.width 302 

將一些細胞關閉屏幕(拖上 - 下沒有做任何事情),當他們再次出現,你得到這個:

2011-06-18 11:10:24.013 TestTableCells[7569:207] cell.contentView.bounds.size.width 320 
2011-06-18 11:10:24.047 TestTableCells[7569:207] cell.contentView.bounds.size.width 320 
2011-06-18 11:10:24.130 TestTableCells[7569:207] cell.contentView.bounds.size.width 320 

坦率地說,我很沮喪與此心裏很不舒服,而且我這樣很動心從腰包裏掏出$ 99(無工作程序,甚至),所以我可以有有人在蘋果權衡這一點。

任何人有什麼是怎麼回事任何想法?

謝謝!


想看看更有趣的東西嗎?現在

NSString *CellIdentifier = [NSString stringWithFormat: @"%d", arc4random() ]; 

    NSLog(@"%@",CellIdentifier); 

,每一次,在日誌中的寬度是始終這似乎,那麼,重用細胞有不同的內容寬度比原來的電池:代替static NSString...線試試這個。

再次...不知道...任何人都得到了線索?

+0

我注意到了一個類似的問題,據此我使用UITableViewCell邊界來創建一個CAGradientLayer來爲每個單元格添加一個漸變。當iPad處於縱向模式時,我看到單元格右側的白色方框,單元格的寬度似乎註冊爲橫向方向寬度。表格滾動和單元格重新使用後,單元格將註冊爲縱向並選取正確的寬度。仍在尋找如何解決它! – robotpukeko 2012-07-18 09:16:39

回答

10

我懷疑你看到的是由於改變細胞的附屬視圖,這將最終調整內容查看下一次的小區進行-layoutSubviews。這應該發生在單元格被添加到表格之前,但在此之前內容視圖的邊界將不會被更新。

無論我不明白爲什麼這會是一個問題。您似乎只關注設置theAgeTextField的寬度,因此如果您相對於內容視圖的當前寬度適當調整其大小,並將它的autoresizingMask標誌設置爲靈活的寬度,那麼它應該根據需要增大或減小,內容視圖更改。

如果您需要更詳細的佈局行爲,那麼這可能全部發生在UITableViewCell子類中。請參閱-prepareForReuse-layoutSubviews,以獲取自定義單元及其子視圖的機會。您的數據源應該能夠將theAge傳遞給您的單元格子類的一個實例,而不必關心它將如何顯示的細節。

+1

我必須根據我編碼的內容來消化你寫的內容。問題在於文本字段中的文本最終在左端被截斷。無論如何,舉個例子,我編輯了這篇文章(這是附件和文本無字段),然後看看你的想法。 – BillEccles 2011-06-18 23:52:19

0

必須實現行委託方法的高度,做所有的計算在裏面

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 

// do math 

return newheight; 
} 

它會給你,你是什麼之後,你還可以使用索引路徑,這樣你就可以很容易地找到在你的數組中進行排序並做你需要的一切,否則你會從隊列中獲得一個重用行的大小,這總是你的默認行大小。

+0

你能解釋爲什麼單元格寬度和單元格的contentView寬度不一樣嗎? – 2014-06-11 08:40:50

+0

由於單元格有更多的視圖和訪問器標籤,如刪除,前進,拖動重新排序,檢查和更多,所以內容視圖不應該是全寬。沒有人禁止你這樣做,但如果你想要tableview的默認行爲,你需要記住一些事情。但是,如果您想要背景圖像或顏色,您可以輕鬆設置背景視圖。 – 2014-06-12 13:01:13

1

我遇到了同樣的情況,只是偏移量是20而不是22。我認爲Jonah的答案是在正確的軌道上,但對於一個快速的解決方法,避免了cell.contentView.frame/bounds的調整行爲使用cell.frame/bounds作爲我在單元格的contentView中佈置視圖的參考。

+1

另一件事:我發現這只是當我用單表格樣式顯示UITableViewStyleGrouped時發生的。如果表格樣式是UITableViewStylePlain,則cell.contentView.bounds.size.width不會更改。 – jay492355 2012-10-11 16:36:57