2012-08-15 19 views
0

我有一個UITableViewCell,當滑動以調出刪除按鈕時,它會擰緊textLabel,因爲我在文本中使用換行符。UITableViewCell刪除按鈕在文本中使用換行符時擰緊textLabel

前:

enter image description here

在刪除模式:

enter image description here

這是令人沮喪,因爲我只是想刪除按鈕來投奔我的文字,而不是試圖製作動畫不要將它擰緊。

  • 這是爲什麼發生?因爲我在我的字符串中使用換行符?
  • 我該如何防止這種情況發生?

回答

0

您應該可以訪問UITableViewCell的textLabel.Frame屬性。子類UITableViewCell並將其設置在[UITableViewCell didTransitionToState:(UITableViewCellStateMask)state]

+0

好吧,所以在didTransitionToState中設置框架? – 2012-08-15 18:50:52

+0

他意味着保存舊的框架,當你轉換到狀態== UITableViewCellStateShowingEditControlMask時,嘗試在那裏設置框架。如果這不起作用,我還有另外一個想法,但是這對你來說可能更簡單(如果有效的話) – 2012-08-15 21:34:01

+0

不,這似乎沒有用。它使事情變得更生動,甚至是陌生人。 – 2012-08-15 22:46:27

1

爲什麼會發生這種情況?因爲我在我的字符串中使用換行符?

它發生的原因是您已將標籤添加到單元格的contentView中(這很好)。當出現刪除按鈕時,contentView將自動調整大小,並且UITableViewCell的textLabel也將調整大小,因爲它的autoresizingMask設置爲UIViewAutoresizingFlexibleWidth。不幸的是,我們無法重寫默認textLabel的autoresizingMask。

我該如何防止這種情況發生?

您必須自己構建自定義單元格。帶有自定義UILabel作爲屬性的UITableViewCell的子類,並將它的自動識別掩碼設置爲UIViewAutoresizingNone

//create and set your frame to whatever you want. 
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 110, 20)]; 

//set autoresizing 
[label setAutoresizingMask:UIViewAutoresizingNone]; 

//add to view 
[[self contentView] addSubview:label]; 

//set property to acces later 
[self myLabel:label]; 

//release (if you aren't using ARC) 
[myLabel release]; 

添加一些示例文本如果您使用InterfaceBuilder中創建視圖後測試它

[[cell myLabel] setText:@"MAX: 72.92 MPH Will fit!"]; 

/細胞確保自動尺寸面具箭頭的是變灰

interfacebuilder autoresizingMask

相關問題