簡體   English   中英

動態單元格高度iOS

[英]Dynamic cell height iOS

我試圖動態地重新計算包含UITextView的單元格的高度。 單元格的高度取決於UITextView的高度。

下面的源代碼重新計算了表格中每個單元格的高度。 這是NSDictionary的一些文字。 重新計算所有高度后,我用包含在heightCells數組中的新高度重新加載了表格視圖;

UITextView *tempTextView = [[UITextView alloc] init];
UIFont *font = [UIFont fontWithName:@"Helvetica" size:16.0];
[tempTextView setFrame:CGRectMake(10, 63, 300, 9999)];
[tempTextView setFont:font];
NSString *str =  [d valueForKey:@"definition"]; // set long text
tempTextView.text = str;
CGRect frame = tempTextView.frame;
frame.size.height = tempTextView.contentSize.height + 20.0;
tempTextView.frame = frame;
[tempTextView sizeToFit];
int height = tempTextView.frame.size.height + 150.0; // set padding 150.0 px
[heightCells addObject:[NSNumber numberWithFloat:height]];

UITableView方法:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [[heightCells objectAtIndex:indexPath.row] floatValue];
}

在非視網膜顯示屏上,一切正常,高度重新計算正確,但是在視網膜顯示屏上,我無法重新計算。 我知道視網膜(而不是視網膜)具有一個320x480點的視圖,並且不應影響重新計算。 但就我而言,這是出現的。

請看下面的截圖。 如您在屏幕快照中看到的,共享按鈕后的底部填充對於視網膜(而不是視網膜顯示)有所不同。 而且我不知道哪種問題。

視網膜

不是視網膜

感謝幫助!

看起來tempTextView的框架在UITableViewCell上時有所不同。 我在這個答案中描述了我的工作技巧。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM