2012-11-05 17 views
3

當前在我的tableView中向上滾動時,我發現了一個奇怪而煩人的錯誤。當我決定釋放「滾動」時,意味着我放棄了「滾動」,所以「視圖」將返回到顯示所有TableView內容的正常位置,因爲某些原因,我的某些單元格可能會在寬度。我不知道爲什麼會發生這種情況,或者是什麼問題。向上滾動後UITableViewCell的高度變化?

我的單元格(commentLabel)是根據我的論壇中標籤的高度自定義爲fitSize。我認爲問題可能在於我如何自定義單元格的內容。我會發布我的相關代碼,併發布到下面的圖片。

開始向上拖動滾動前:http://tinypic.com/view.php?pic=2rfsum9&s=6

發佈後/再DROP掉滾動到其正常位置。現在,小區的一個變化:http://tinypic.com/view.php?pic=swxnqv&s=6

代碼:

- (UITableViewCell *)tableView:(UITableView *)pTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"ForumthreadCell"; 
    UITableViewCell *cell = [pTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    Feedback *item = [self.items objectAtIndex:indexPath.row]; 

    UILabel *aliasLabel = (UILabel *)[cell viewWithTag:1]; 
    UILabel *commentLabel = (UILabel *)[cell viewWithTag:2]; 
    UILabel *dateLabel = (UILabel *)[cell viewWithTag:3]; 

    [aliasLabel setText:item.alias]; 
    [commentLabel setText:item.comment]; 
    [dateLabel setText:[self.dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:(double)item.time]]]; 

    commentLabel.numberOfLines = 0; 
    [commentLabel sizeToFit]; 

    return cell; 

} 

-(CGFloat)getLabelHeightForText:(NSString *)text andWidth:(CGFloat)labelWidth 
{ 

    CGSize maximumSize = CGSizeMake(labelWidth, 10000); 

    //provide appropriate font and font size 
    CGSize labelHeighSize = [text sizeWithFont: [UIFont fontWithName:@"Trebuchet MS" size:12.0f] 
        constrainedToSize:maximumSize 
         lineBreakMode:UILineBreakModeTailTruncation]; 
    return labelHeighSize.height; 

} 

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

    Feedback *item = [self.items objectAtIndex:indexPath.row]; 

    CGFloat commentTextHeight = [self getLabelHeightForText:item.comment andWidth:162]; 
    return commentTextHeight + 39; 

} 

編輯

新代碼:

CGRect frame = commentLabel.frame; 
frame.size.width = kLabelWidth; 
frame.size.height = 10000; 
commentLabel.frame = frame; 

frame = commentLabel.frame; 
frame.size.height += kOffset; 
commentLabel.frame = frame; 

但我做錯了什麼SI現在從commentLabel的文本沒有得到正確顯示,任何人都可以在我的錯誤在這裏如何設置框架?這是細胞的樣子現在http://oi50.tinypic.com/2w5u0s6.jpg

新全碼:

static const CGFloat kLabelWidth = 162; 
static const CGFloat kOffset = 39; 

- (UITableViewCell *)tableView:(UITableView *)pTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"ForumthreadCell"; 
    UITableViewCell *cell = [pTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    Feedback *item = [self.items objectAtIndex:indexPath.row]; 

    UILabel *aliasLabel = (UILabel *)[cell viewWithTag:1]; 
    UILabel *commentLabel = (UILabel *)[cell viewWithTag:2]; 
    UILabel *dateLabel = (UILabel *)[cell viewWithTag:3]; 

    [aliasLabel setText:item.alias]; 
    [commentLabel setText:item.comment]; 
    [dateLabel setText:[self.dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:(double)item.time]]]; 

    CGRect frame = commentLabel.frame; 
    frame.size.width = kLabelWidth; 
    frame.size.height = 10000; 
    commentLabel.frame = frame; 

    frame = commentLabel.frame; 
    frame.size.height += kOffset; 
    commentLabel.frame = frame; 

    commentLabel.numberOfLines = 0; 
    [commentLabel sizeToFit]; 

    return cell; 

} 
+0

in'heightForRowAtIndexPath' getLabelHeightForText'返回什麼?有沒有什麼機會'item.comment'可以是零,它返回零?請記住,細胞被重新使用。 – yfrancis

回答

3

你應該重用細胞前調整標籤幀。在你的情況正在改變細胞的高度,同時在

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

重用,但你也應該調整根據小區的身高標籤的框架,使其不會超出電池的束縛。

+0

嘗試這個 - [commentLabel SETFRAME:CGRectMake(commentLabel.frame.origin.x,commentLabel.frame.origin.y,kLabelWidth,[自getLabelHeightForText:item.comment andWidth:kLabelWidth)] –

+0

感謝名單的答案,我現在的問題是細胞的高度,出於某種原因,它們似乎是完全隨機的。 http://oi50.tinypic.com/2w5u0s6.jpg對此有何想法? – user1293618

+0

您可以移除您在heightForRowAtIndexPath中設置的offset(commentTextHeight + 39),也可以在commentLabel的高度中添加相同的偏移量。 [commentLabel setFrame:CGRectMake(commentLabel.frame.origin.x,commentLabel.frame.origin.y,kLabelWidth,[self getLabelHeightForText:item.comment andWidth:kLabelWidth] + 39)] [0124] –

相關問題