设置uitableviewcell中textlabel的位置?

时间:2012-10-25 13:30:53

标签: iphone ios ipad

在我的应用程序中,我试图在tableview单元格中调整textlabel和detailtextlabel的位置。我这样做了。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
     static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cell_with_arrow.png"]];
        [cell setBackgroundView:img];
        [img release];  


    }

    cell.textLabel.frame = CGRectMake(0,0, 150, 40);
    cell.detailTextLabel.frame = CGRectMake(155, 55, 150, 40);
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14.0];
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.textLabel.text =@"name";
    cell.detailTextLabel.text=@" status";

     cell.detailTextLabel.backgroundColor = [UIColor clearColor]; 
    //cell.detailTextLabel.textAlignment = UITextAlignmenteft;
    //cell.textLabel.textAlignment = UITextAlignmentLeft;

  return cell;
}

`但似乎没有效果。任何人帮我指出我哪里出错了?

3 个答案:

答案 0 :(得分:3)

textLabeldetailTextLabelreadonly,因此您无法更改它们。

@property(nonatomic, readonly, retain) UILabel *textLabel
@property(nonatomic, readonly, retain) UILabel *detailTextLabel

请参阅here

答案 1 :(得分:2)

我建议您可以创建自定义UITableViewCell,您可以更好地控制其内容,也可以在其中添加自定义子视图。

以下是一些教程:

From IB

From Code

答案 2 :(得分:2)

您需要覆盖layoutSubviews方法

创建一个customcell类并使用uitableviewcell继承

@try this code

<强> CustomCell.h

#import <UIKit/UIKit.h>

@interface CustomCell : UITableViewCell

@end

<强> CustomCell.m

#import "CustomCell.h"

@implementation CustomCell

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

-(void)layoutSubviews{

    //Set here your frame

    self.textLabel.frame = CGRectMake(0, , 320, 30);
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
相关问题