2012-01-30 60 views
1
  • 在我的表视图我使用UITableviewCEllStyleValue1:

if (cell == nil) 
     { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease]; 

    cell.textLabel.text = @"Hai"; 
       cell.detailTextLabel.text = @"Hello"; 

     cell.detailTextLabel.textAlignment = UITextAlignmentLeft; 
     cell.textLabel.textAlignment = UITextAlignmentLeft; 
     return cell; 

     } 

  • 这里有两个标签文本拉贝,detailTextLabel显示。如何设置为textLabel的位置在的tableview细胞iphone

  • 但不在我想要的位置。

  • 我需要从单元格的起始位置(0,0)显示Textlable,并且immidiate右侧的位置是detailTextlabel,两个标签之间没有间隔。

  • 我需要使用Allignment的解决方案。该怎么办?

回答

-6

您必须为此创建自定义单元格。这样,

在CustomCellTableViewCell.h

文件

#import <UIKit/UIKit.h> 

@interface CustomCellTableViewCell : UITableViewCell 
@property(nonatomic,strong)UILabel *lblUnit; 
@end 
in CustomCellTableViewCell.m file 
#import "CustomCellTableViewCell.h" 

@implementation CustomCellTableViewCell 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    self.userInteractionEnabled = YES; 
    if (self) { 
     // Initialization code 
     self.lblUnit = [[UILabel alloc]init]; 
     [self.lblUnit setTextColor:[UIColor whiteColor]]; 
      [self.lblUnit setFont:[UIFont fontWithName:@"Roboto-Light" size:30.0f]]; 
     // [self.lblUnit setFont:[UIFont systemFontOfSize:18.0]]; 
     self.lblUnit.textAlignment = UIBaselineAdjustmentAlignCenters; 
     self.lblUnit.adjustsFontSizeToFitWidth = YES; 
     [self.contentView addSubview:self.lblUnit]; 
     } 
    return self; 
} 

@end 
in your Tableview CellForRowAtIndexpath write below code 
 cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 

    if(cell==nil){ 
     cell = [[CustomCellTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 
    } 

    cell.lblUnit.frame = CGRectMake(0, 10, 150, 40); 
    cell.lblUnit.text = @"Hai"; 
    return cell; 
+0

我曾试过,但现在看到上面的位置 – 2012-01-30 09:25:27

+0

没有变化。我只是编辑它。 – 2012-01-30 09:41:56

+4

默认cell.textlabel不可移动 – Anthony 2012-11-16 08:29:26

8

创建一个自定义UITableViewCell子类并覆盖layoutSubviews方法。

0

如果您使用initWithStyle:UITableViewCellStyleDefault,则NSTextAlignmentCenter可以正常工作。