mardi 21 juin 2016

iOS customize tableviewcell, the separator length is wrong


I have customized a tableview cell programmatically.

header file:

#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

implementation file:

#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

And the result is shown like image

As you see, the cell separator is short on the right side.

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

this method is able to work around but I think there is mistakes on my method to customizing the tableview cell.

Thank you


Aucun commentaire:

Enregistrer un commentaire