UITableViewCell标签显示不正确

时间:2015-01-08 06:52:42

标签: ios objective-c uitableview

我希望我的标签稍微间隔开,并且可以提供多行文字。

Cell.m

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


- (void)layoutSubviews {

    [super layoutSubviews];
    self.imageView.frame = CGRectMake(1,1,69,69);
    float limgW =  self.imageView.image.size.width;
    if(limgW > 0) {
        self.textLabel.frame = CGRectMake(74,self.textLabel.frame.origin.y,self.textLabel.frame.size.width,self.textLabel.frame.size.height);
        self.detailTextLabel.frame = CGRectMake(74,self.detailTextLabel.frame.origin.y+self.textLabel.frame.size.height,self.detailTextLabel.frame.size.width,self.detailTextLabel.frame.size.height);
    }

}

在控制器中,cellForRowAtIndexPath为:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
                        object:(PFObject *)object
{
    static NSString *CellIdentifier = @"Cell";

    Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[Cell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }


    // Configure the cell to show todo item with a priority at the bottom
    cell.textLabel.text = object[@"Title"];
    cell.detailTextLabel.text = object[@"Request"];
    PFFile *thumbnail = object[@"ProfilePic"];
    cell.imageView.image = [UIImage imageNamed:@"AppIcon60x60@2x.png"];
    cell.imageView.file = thumbnail;
    CALayer * l = [cell.imageView layer];
    [l setMasksToBounds:YES];
    [l setCornerRadius:36.7];
    [l setBorderWidth:2.0];
    [l setBorderColor:[[UIColor whiteColor] CGColor]];
    UIFont *cellFont = [UIFont fontWithName:@"Verdana-Bold" size:15];
    UIFont *cellFont2 = [UIFont fontWithName:@"Verdana-Bold" size:12];
    cell.textLabel.numberOfLines = 0;
    cell.detailTextLabel.numberOfLines = 0;
    cell.textLabel.font = cellFont;
    cell.detailTextLabel.font = cellFont2;
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.detailTextLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor blackColor];


    return cell;
}

为什么第二行会缩短,为什么textLabeldetailTextLabel如此接近? enter image description here

2 个答案:

答案 0 :(得分:1)

使用自定义UITableViewCell,

例如:

@interface CustomViewCell : UITableViewCell

@property(nonatomic,weak)IBOutlet UIImageView *imageView;
@property(nonatomic,weak)IBOutlet UILabel *titleView;
@property(nonatomic,weak)IBOutlet UILabel *detailView;

@end

答案 1 :(得分:0)

尝试:

 if(limgW > 0) {
        self.textLabel.frame = CGRectMake(74,self.textLabel.frame.origin.y,self.textLabel.frame.size.width,self.textLabel.frame.size.height);

        // change this two lines
        self.detailTextLabel.frame = CGRectMake(74,self.detailTextLabel.frame.origin.y,self.detailTextLabel.frame.size.width,self.detailTextLabel.frame.size.height * 2);
        self.detailTextLabel.numberOfLines = 2;
    }