2012-08-30 36 views
0

對齊文本我在的tableView正常細胞,當我要對齊文本正確使用:問題與UITableViewCell的

if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];   
    cell.textLabel.textAlignment = UITextAlignmentRight;   
} 

但現在我想用UITableViewCell爲還顯示detailTextLabel我試着它便又對齊,但沒有任何反應:

if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.textLabel.textAlignment = UITextAlignmentRight; 
    cell.detailTextLabel.textAlignment = UITextAlignmentRight;  
} 

回答

3

添加自己的UILabel作爲一個子視圖到小區,如果你想自定義:

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease]; 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
cell.textLabel.textAlignment = UITextAlignmentRight; 
UILabel *subLabel = [UILabel alloc] initWithFrame:CGRectMake(x,y,w,h)]; //put in the frame variables you desire 
subLabel.textAlignment = UITextAlignmentRight; 
subLabel.tag = 20; 
subLabel.text = @"my text"; 
[cell.contentView addSubview:subLabel]; 
[subLabel release]; //dont call this if you are using ARC 

...

那麼您以後訪問標籤:

[[cell.contentView viewWithTag:20] setText:@"new text"]; 

希望這有助於。

+0

我同意。內置標籤有時會出現意想不到的行爲,有時會進行對齊和定位我知道在過去,我遇到過一個問題,就是在調整單元格大小時,內置標籤是如何自動重新對齊的,但創建的標籤不會自動重新對齊(這是我實際想要的行爲)。 – MikeS

+0

@KDaker這是他唯一的結果嗎?因爲我想插入那裏動態文本,可以是不同的大小,我需要初始化它的CGRectMake的一些參數,我不知道,當我得到的文本 – vadim

+0

你是否正在更改文本,而單元格是在視圖?或者當你回到它?因爲您仍然可以通過標記文本而不通過實例變量來分配文本。 – KDaker

0

嘗試實施tableView:willDisplayCell:forRowAtIndexPath:委託方法,並把你重新對齊的代碼在那裏!

1

您是否檢查了detailTextLabel的長度是否不像框架那樣大?如果框架剛好足夠大以準確地匹配文本,那麼它可能看起來像UITextAlignmentRight不起作用。

+0

您的意思是文本的長度?爲了測試,我只是用一個charcter初始化它。 – vadim

0

這是不可能在UITableViewCellStyleSubtitle表格單元格以簡單的方式來改變幀爲textLabeldetailTextLabel

最好將您的單元劃分爲自定義佈局。

[self performSelector:@selector(alignText:) withObject:cell afterDelay:0.0]; 

alignText::但你也可以之後layoutSubviews實現使用

performSelector:withObject:afterDelay: 

與零延遲改變爲textLabel幀小黑客你可以重新定義爲textLabel/detalTextLabel幀。

請參閱here的實施