2012-09-11 51 views
0

我看到了類似的問題是問幾乎數十次重疊在下一單元格,但沒有答案的解決我的問題(或者我只是太累了,所以我不回答如下正確)多行標籤滾動時快速

我有一個自定義的細胞的tableview。

細胞看起來像這樣

My custom cell in storyboard

所述細胞類型ResultsViewCell,它是從定義UITableViewCell衍生的。

單元格中的最後一個標籤是多行單元格。那個有時會與下一個單元格的內容重疊。

這裏是我的cellForRowAtIndexPath功能

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

     ResultsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     // Configure the cell... 
     Data *theData = [Data getInstance]; 
     Company *theCompany = [theData.results objectAtIndex:indexPath.row]; 

     cell.lblTitle.text = theCompany.DisplayName; 

     cell.lblDescription.text = theCompany.Description; 
     cell.lblAddressPt1.text = theCompany.AddressPt1; 
     cell.lblAddressPt2.text = theCompany.AddressPt2; 
     cell.lblPhone.text = theCompany.Phone; 
     cell.lblEmail.text = theCompany.Email; 

     cell.lblDescription.adjustsFontSizeToFitWidth = false; 
     cell.lblDescription.lineBreakMode = UILineBreakModeWordWrap; 
     cell.lblDescription.numberOfLines = 0; 
     [cell.lblDescription sizeToFit]; 

     /////////////////////////////////////////// 
     //edit -Added after David H's answer, but it didn't solve the problem 
     cell.contentView.clipsToBounds = false; 
     UIFont *cellFont = [UIFont systemFontOfSize:12.0]; 
     CGSize constraintSize = CGSizeMake(270.0f, MAXFLOAT); 
     CGSize labelSize = [theCompany.Description sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 
     [cell.contentView setFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, 270, 90 + labelSize.height)]; 
     //end of edit 
     /////////////////////////////////////// 

     return cell; 
    } 


- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    Data *theData = [Data getInstance]; 
    Company *theCompany = [theData.results objectAtIndex:indexPath.row]; 

    UIFont *cellFont = [UIFont systemFontOfSize:12.0]; 
    CGSize constraintSize = CGSizeMake(270.0f, MAXFLOAT); 
    CGSize labelSize = [theCompany.Description sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

    return (90.0f + labelSize.height); 
} 

我在做什麼錯?

+0

哈哈,我會試着更注意這一點。如果可以的話,現在請幫助我。爲這個警告投票。 – Eedoh

+0

同意@DavidH答案。但是我很驚訝這段代碼沒有處理deque返回nil的情況。爲了簡潔起見,您是否將其從粘貼中取出? – danh

+0

@丹說得很好 - 我沒有真正看待整個方法。通常你會回收細胞。 –

回答

1

你可能會吃驚,但對於UIView的財產「clipsToBounds」默認爲NO - 也就是說,如果子視圖伸過視圖的框架,讓他們無論如何。

解決方法是,以保證沒有這些觀點有「clipsToBounds」設置爲NO。您應該將它設置爲cell.contentView爲YES,並確保cell.contentView的框架具有與您在委託的'heightForRowAtIndexPath:'方法中報告相同的高度。

您可能還需要到「clipsToBounds」設置爲YES,多標籤(不知道,可能不是)。

+1

我認爲cellView應該足夠了,因爲屬性處理子視圖的處理。 +1像往常一樣。 – danh

+0

我試過我以爲你讓我做的事。它沒有幫助。我編輯了原始文章,以便您可以看到我做了什麼。 – Eedoh

+0

clipsToBounds = YES - 即,您希望容器視圖剪輯將在視圖外繪製的任何子視圖。我在發佈它之後編輯了我的帖子,使其更加清晰 - 您可能已經看到第一份草案沒有說清楚。 –

0

則需要基於該標籤的文字高度來調整你的tableViewCell高度。例如採用這種方法:

- (int)textHeight:(NSString *)text { 
    int width = self.tableView.frame.size.width; 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 30)]; 
    label.numberOfLines = kMyNumberOfLines; 

    //setup the label to look like the label of the cell 

    label.text = text; 
    [label sizeToFit]; 

    int h = (int)label.frame.size.height; 
    [label release]; 
    return h; 
} 

然後使用該方法在heightForRowAtIndexPath,以確定您的單元格的高度。

0

按照這樣的...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *myCell=[tableView dequeueReusableCellWithIdentifier:@"hi"]; 
    myCell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"hi"]; 

    if (myCell==nil) 
    { 
     NSArray *viewsToRemove = [mytable1 subviews]; 

     for (UITableView *table in viewsToRemove) 
     { 
      [table removeFromSuperview]; 
     } 
    }  
    return myCell; 
}