2013-05-28 20 views
4

我創建了一個自己擁有tableview類的項目。 我有3件事在tableview的任何單元格。 (1個按鈕(左邊)和2拉布勒狀底部圖像)如何在單元格tableview上對齊UILabel?

enter image description here

我想標題單元格是右對齊,但我不能這樣做。

這是我的代碼。哪裏是我的錯誤:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) 
    { 
     _backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"copymove-cell-bg"]]; 
     [_backgroundImageView setContentMode:UIViewContentModeTopRight]; 
     [self setBackgroundView:_backgroundImageView]; 
     [self setSelectionStyle:UITableViewCellSelectionStyleNone]; 

     _iconButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [_iconButton setFrame:CGRectMake(0, 10, 47, 50)]; 
     [_iconButton setAdjustsImageWhenHighlighted:NO]; 
     [_iconButton addTarget:self action:@selector(iconButtonAction:) forControlEvents:UIControlEventTouchUpInside]; 
     [_iconButton setImage:[UIImage imageNamed:@"item-icon-folder"] forState:UIControlStateNormal]; 
     [_iconButton setImage:[UIImage imageNamed:@"item-icon-folder-selected"] forState:UIControlStateSelected]; 
     [_iconButton setImage:[UIImage imageNamed:@"item-icon-folder-selected"] forState:UIControlStateHighlighted]; 
     [self.contentView addSubview:_iconButton]; 

     _titleTextField = [[UILabel alloc] initWithFrame:CGRectMake(69, 29, _titleTextField.frame.size.width, _titleTextField.frame.size.height)]; 
     [_titleTextField setFont:KOFONT_FILES_TITLE]; 
     [_titleTextField setTextColor:KOCOLOR_FILES_TITLE]; 
     [_titleTextField.layer setShadowColor:KOCOLOR_FILES_TITLE_SHADOW.CGColor]; 
     [_titleTextField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin]; 
     [_titleTextField.layer setShadowOffset:CGSizeMake(0, 1)]; 
     [_titleTextField.layer setShadowOpacity:1.0f]; 
     [_titleTextField.layer setShadowRadius:0.0f]; 
     [_titleTextField setTextAlignment:UITextAlignmentRight]; 
     [_titleTextField setLineBreakMode:UILineBreakModeMiddleTruncation]; 
     [self.contentView addSubview:_titleTextField]; 
     [self.layer setMasksToBounds:YES]; 

     _countLabel = [[UILabel alloc] initWithFrame:CGRectMake(273, 29, 47, 28)]; 
     [_countLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin]; 
     [_countLabel setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"item-counter"]]]; 
     [_countLabel setTextAlignment:UITextAlignmentCenter]; 
     [_countLabel setLineBreakMode:UILineBreakModeMiddleTruncation]; 
     [_countLabel setFont:KOFONT_FILES_COUNTER]; 
     [_countLabel setTextColor:KOCOLOR_FILES_COUNTER]; 
     [_countLabel setShadowColor:KOCOLOR_FILES_COUNTER_SHADOW]; 
     [_countLabel setShadowOffset:CGSizeMake(0, 1)]; 

     [self setAccessoryView:_countLabel]; 
     [self.accessoryView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleBottomMargin]; 
    } 
    return self; 
} 
+0

U可以嘗試有針對的TableView細胞筆尖好讓你不必給代碼框架和代碼將是一個很多清潔劑。 –

+0

嘗試使用相同的標籤寬度? – tom19830924

+0

@ tom19830924我這樣做,但不工作:( – david

回答

1

如果使用的是iOS6的,

使用[_countLabel setTextAlignment:NSTextAlignmentCenter];因爲UITextAlignmentCenter在iOS6的棄用。

要右對齊,

[_countLabel setTextAlignment:NSTextAlignmentRight]; 

希望這有助於你...

+0

我的朋友我使用ios 5 – david

+0

使用UITextAlignmentRight而不是NSTextAlignmentRight ... – lakesh

0

您的代碼在我結束工作的罰款。請檢查您傳遞給標籤的寬度和高度。通過設置標籤的寬度和高度來檢查你的代碼。

希望它可以幫助你。

0
_titleTextField.contentHorizontalAlignment = 
     UIControlContentHorizontalAlignmentRight 
0

首先UITextAlignmentRightdeprecatedin iOS6,所以你應該使用:

_titleTextField.textAlignment = 2; 

其次,你需要檢查你的UILabel Frame。請檢查您的UILableCGFloat x, CGFloat ywidth

textAlignment = 0(LeftAlignment)。 textAlignment = 1(CenterAlignment)。 textAlignment = 2(RightAlignment)。

希望它有幫助。

0

如果對齊標籤內的文本是您的問題,那麼上面的解決方案是很好的。 如果設定框架是什麼原因呢,我會建議保持代碼乾淨的東西喜歡 -

有一個nibCell.xib文件和相應的.m.hCell.mCell.h文件,該文件是筆尖文件的所有者。 Cell類應該是UITableViewCell的子類。將UILables/UIImageView/UIButton放置在xib的所需位置,並將它們映射到Cell類中的相應屬性(也可以在Cell.xib中給出一些Cell identifier)。

cellForRowatIndexdatasource方法來獲得從筆尖細胞喜歡 -

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    Cell *cell = (Cell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];//same CellIdentifier as in the Cell.xib 
        if(cell==nil){ 
       cell = [[[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self  options:nil] lastObject]; 
      } 

     //Access and set the labels, imageViews here 

      return cell; 
     } 
0
_titleTextField = [[UILabel alloc] initWithFrame:CGRectMake(69, 29, _titleTextField.frame.size.width, _titleTextField.frame.size.height)]; 

這是奇怪的。您正在根據自己的寬度和高度設置標籤的寬度和高度。這會將其設置爲之前標籤的寬度和高度。我不知道你第一次遇到這個代碼會做什麼。

您的文本在標籤視圖的邊界內右對齊,但邊界不會在您想要的位置結束。您可以嘗試設置背景顏色,以便您可以看到標籤的框架位置。

_titleTextField.backgroundColor = [UIColor yellowColor]; 

您應該將其設置爲填充單元格的空間,並讓它定位文本內的文本。使標籤的框架太大不會影響文本的大小,儘管太小可能會取決於所設置的選項。

_titleTextField = [[UILabel alloc] initWithFrame:CGRectMake(69, 29, 273 - 69, self.contentview.bounds.size.height)] 
0

對於iOS 10+ &斯威夫特3+

titleTextField.textAlignment = .center 
lblTitle.textAlignment = .right