2015-05-15 135 views
0

UITableViewCell with two Dynamic UILabels的UITableViewCell/UITableView的:在UITableViewCell中2個UILabels動態調整閃爍

我要在圖像中顯示一些像這樣的事情在兩個UILabels垂直旁邊基於文本的另一個擴大。

這就是我試圖做的。

對於label1,我使用NSMutableAttributedString,以便我可以設置行間距。 Label2與正常的字符串文本。 )的UITableView的

屬性

tableView.estimatedRowHeight = 110 
tableView.rowHeight = UITableViewAutomaticDimension 

heightForRowAtIndexPath(未實現爲這些屬性將採取高度的護理動態

在的cellForRowAtIndexPath()

var paragraphStyle = NSMutableParagraphStyle() 
paragraphStyle.lineSpacing = 15 
paragraphStyle.alignment = NSTextAlignment.Right 
attrString = NSMutableAttributedString(string: "HUGE TEXT 1") 
attrString.addAttribute(NSParagraphStyleAttributeName,  value:paragraphStyle, range:NSMakeRange(0, attrString.length)) 
myCell.label1.attributedText = attrString 
myCell.label2.text = "HUGE TEXT 2" 

問題:當向下滾動(來自下方的新細胞)沒有問題,但是當我向上滾動時(新細胞從上向下),細胞開始閃爍,並且t母雞調整大小以適應卷軸的末尾。閃爍僅在UILabel2上。對於這種佈局,我似乎沒有發現堆棧溢出的解決方案。

回答

0

這可能會幫助你,在你的UITableViewCell實現此方法:

+ (CGFloat) cellHeightForContent:(NSString)aContent { 
    CGFloat result = 0; 
    CGSize textDims = [aContent getCorrectSizeWithFont:[UIFont fontWithName:@"Helvetica" size:13] constrainedToSize:CGSizeMake(kLabelWidth, CGFLOAT_MAX)]; 
    result = 2 * kMarginHeight + textDims.height; 

    return result; 
} 

通過此方法返回的值將heightForRowAtIndexPath方法使用。

之後,你需要定義方法getCorrectSizeWithFont在一個NSString類別,這樣的:

- (CGSize)getCorrectSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size 
{ 
    NSMutableDictionary *attrsDictionary = [NSMutableDictionary dictionaryWithObject:font forKey:NSFontAttributeName]; 

    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; 
    [attrsDictionary setObject:paragraphStyle forKey:NSParagraphStyleAttributeName]; 

    NSMutableAttributedString* attrStrWithLinks = [[NSMutableAttributedString alloc] initWithString:self attributes:attrsDictionary]; 
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrStrWithLinks); 
    CGSize sz = CTFramesetterSuggestFrameSizeWithConstraints(framesetter,CFRangeMake(0,0),NULL,CGSizeMake(size.width,CGFLOAT_MAX),NULL); 
    if (framesetter) CFRelease(framesetter); 
    return CGSizeMake(sz.width,sz.height); 
}