2013-07-22 46 views
2

我有一個自定義UITableViewCellUILabelStoryboard。我動態地設置了這個UILabel的文本,我需要相應地調整它的寬度,因爲在這之後我有另一個必須保持並排的UILabelUILabel動態寬度與單元格上的sizeToFit

我與

> MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
> author = @"Some dynamic text"; 
> cell.authorLabel.text = author; 
> [cell.authorLabel sizeToFit]; 

會發生什麼努力: 在第一渲染UILabel只是不斷被上storyboard設置寬度,因此無論是文本裁剪或之後有太多的空間。

滾動後,單元格出列並正確應用sizeToFit方法,但已經太遲了,因爲第二個UILabel已被放置在錯誤的位置。 (由於名譽限制較低,無法發佈圖片)

任何想法?

+0

見[這個問題](http://stackoverflow.com/questions/1947970/dynamic-calculation-of-uilabel-width-in-uitableviewcell?rq=1 ) –

回答

1

使用這一個獲得動態的高度和寬度,

CGSize sizeDynamic = [str sizeWithFont:[UIFont fontWithName:@"Arial-BoldMT" size:14] constrainedToSize:CGSizeMake(CGFLOAT_MAX,CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping]; 

,你可以得到,

sizeDynamic.height and sizeDynamic.width values 
+0

這可以根據內容正確調整'UILabel'的大小,但我仍然遇到這樣的問題:並排移動標籤不會移動,因此它不會考慮水平間距約束和最終結果是兩個文本重疊。我應該根據第一個標籤的新寬度來計算第二個標籤的新位置嗎? N.B.如果這個'sizeWithFont'工作,並且同一點上的'sizeToFit'不起作用,我會忽略'sizeToFit'方法? – stilllife

0

也許你的意思是這樣的:

MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
author = @"Some dynamic text"; 
cell.authorLabel.text = author; <-- excluded in your code 
[cell.authorLabel sizeToFit]; 

如果沒有按」噸工作,嘗試使用這樣的一些:

子類的UITableViewCell並重寫如下方法:

- (void)setSelected:(BOOL) selected 
{ 
    [super setSelected:selected]; 
    [self.authorLabel sizeToFit]; 
} 
+0

抱歉忘記您正確添加的線路。無論如何,這仍然不能解決問題 – stilllife