2012-01-20 59 views
0

我有一個UITableView,我想在一行中應該右對齊的文本和另一個應該左對齊的文本。這兩個文本應該從行邊界的一些空間開始。我不希望他們從行的邊界開始。我怎樣才能做到這一點?如何在tableview行中編寫兩個或多個文本並將它們放置在不同的位置?

enter image description here

我想每艘左對齊,也是一個文本是正確的alighned行中寫入文本。

在此先感謝。

+0

你能顯示一些圖像嗎? – Sarah

+0

對不起,我錯誤地寫了標籤。它的表格的行。 – Piscean

+0

您可以藉助自定義單元格並在您的桌面視圖中實現相同的功能,並根據您的喜好使其看起來相同。有幾個鏈接可用:我最喜歡的是http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html和http://iphone.galloway.me.uk/iphone-sdktutorials/custom-uitableviewcell /。希望能幫助你。 – Sarah

回答

1

UILabel在造型和佈局方面非常有限。考慮使用多個佈局或CoreText。

1

你可以做的是子類的標籤,在寫入其drawtextinrect功能,像這樣

@interface UILabelCustom : UILabel 

- (void)drawTextInRect:(CGRect)rect; 
@end 


@implementation UILabelCustom 

-(void)drawTextInRect:(CGRect)rect{ 
    UIEdgeInsets insets = {5, 5, 5, 5}; 
    return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)]; 
} 

@end 

,這將讓你給周圍的標籤 邊緣的保證金,你也可以創建一個包含一個UIView 2個自定義標籤並左右對齊。使用uvview作爲2個標籤的容器來根據需要定位它們。我認爲這可能會創造你尋找的效果

編輯:剛剛看到你的編輯是爲表格單元格。你需要的UILabel

@interface CustomCell : UITableViewCell { 

} 


@property (retain, nonatomic) UILabelCustom *label1,*label2; 


@end 

@implementation CustomCell 

@synthesize label1, label2; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

- (void)dealloc 
{ 

    [label1 release]; 
    [label2 release]; 


    [super dealloc]; 
} 

@end 
+0

你的意思是說,我應該在每個細胞中添加兩個標籤,然後將它們定位爲我想要的? – Piscean

+0

是的,你會定位他們一次在自定義單元格中的IB或代碼然後當表加載該自定義單元格,他們將是你想要的地方 – glogic

2

創建一個自定義單元格以及自定義在你的cellForRowAtIndexPath方法只是創建2個UILabels細胞,並給他們所需的位置。小例子:

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]; 
}  
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 50, 20)]; 
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(80, 10, 50, 20)]; 
    label1.textAlignment = UITextAlignmentLeft; 
    label2.textAlignment = UITextAlignmentRight; 
    //other labels customization and add them to your cell 
    return cell; 
} 

沒有附近的SDK,所以可能會有一些錯誤。

希望它有幫助

相關問題