0

在我的应用程序中,我正在尝试调整 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;
}

`但似乎没有效果。谁能帮我指出我哪里出错了?

4

3 回答 3

3

textLabel所以你不能改变它们detailTextLabelreadonly

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

这里

于 2012-10-25T13:34:02.713 回答
2

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

这里有一些教程:

来自 IB

从代码

于 2012-10-25T13:34:19.127 回答
2

你需要覆盖 layoutSubviews 方法

创建一个customcell类并用uitableviewcell继承

@试试这段代码

自定义单元格.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
于 2012-10-25T13:38:43.360 回答