iOS自定义tableviewcell,分隔符长度错误

时间:2016-06-21 01:53:39

标签: ios objective-c uitableview

我已经以编程方式定制了一个tableview单元格。

头文件:

#import <UIKit/UIKit.h>
@class LoadingCell;

typedef NS_ENUM(NSUInteger, LoadingCellStyle) {
    LoadingCellStyleSelection,
    LoadingCellStyleTextField,
    LoadingCellStyleImagePicker,
};

@interface LoadingCell : UITableViewCell

@property (nonatomic, strong) UIImageView* leftImageView;
@property (nonatomic, strong) UILabel* titleLabel;
@property (nonatomic, strong) UILabel* detailLabel;

@property (nonatomic, assign) LoadingCellStyle style;

-(instancetype)initWithStyle:(LoadingCellStyle)style;

@end

实施档案:

#import "LoadingCell.h"

#define itemHeight 44

@implementation LoadingCell

-(instancetype)initWithStyle:(LoadingCellStyle)style
{
    self = [super initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"loadingStats"];
    self.style = style;

    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.leftImageView];

    if (self.style == LoadingCellStyleSelection) {
    }
    return self;
}

#pragma mark property setter
-(UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = COLOR_TEXT_GRAY;
        _titleLabel.font = [UIFont systemFontOfSize:13];
    }
    return _titleLabel;
}

-(UILabel *)detailLabel
{
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc] init];
        _detailLabel.textColor = COLOR_TEXT_GRAY;
    }
    return _detailLabel;
}

-(UIImageView *)leftImageView
{
    if (!_leftImageView) {
        _leftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"star"]];
    }
    return _leftImageView;
}

#pragma mark layouts
-(void)layoutSubviews
{
    CGRect imageframe = CGRectMake(k_Margin, 0, itemHeight, itemHeight);
    [self.leftImageView setFrame:imageframe];
    [self.titleLabel setFrame:CGRectMake(CGRectGetMaxX(imageframe), 0, 100, itemHeight)];
    [self.detailLabel setFrame:CGRectMake(CGRectGetMaxX(self.titleLabel.frame), 0, kScreen_Width-CGRectGetMaxX(self.titleLabel.frame)-50, itemHeight)];
}

@end

结果如图所示 image

如您所见,细胞分离器在右侧较短。

self.separatorInset = UIEdgeInsetsMake(0, 0, 0, -100);

这种方法可以解决,但我认为自定义tableview单元格的方法存在错误。

谢谢

1 个答案:

答案 0 :(得分:0)

解决方案:我错过了

[super layoutSubviews];

在我自定义的布局方法中。