//參考這位博友的博客 http://www.cnblogs.com/kenshincui/p/3931948.html
html
自定義cell的 .h 文件 ide
#import <UIKit/UIKit.h> ui
@interface CustomTableViewCell : UITableViewCell atom
@property (nonatomic, assign) BOOL isFirst; spa
@property (nonatomic, assign) BOOL isLast; code
@property (nonatomic, strong) UIImageView *flowStateImageView; orm
@property (nonatomic, strong) UIView *lineView; htm
-(void)setContentWithDictionary:(NSMutableDictionary *)dic; blog
+(CGFloat)getHeightWithString:(NSString *)str size:(CGSize)size; ip
@end
自定義cell的 .m 文件
#import "CustomTableViewCell.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kScreenBounds [UIScreen mainScreen].bounds
@interface CustomTableViewCell ()
@property (nonatomic, strong) UILabel *titleLable;
@property (nonatomic, strong) UILabel *remarkLable;
@property (nonatomic, strong) UILabel *timeLable;
@end
@implementation CustomTableViewCell
- (void)awakeFromNib {
// Initialization code
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self customUI];
}
return self;
}
-(void)customUI
{
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.titleLable = [[UILabel alloc] initWithFrame:CGRectMake(50, 13, kScreenWidth - 50 - 15,( 100 - 13 - 9 - 9 - 13)/3.0)];
_titleLable.textColor = [UIColor blackColor];
_titleLable.font = [UIFont systemFontOfSize:15];
_titleLable.backgroundColor = [UIColor redColor];
[self addSubview:_titleLable];
self.remarkLable = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.titleLable.frame),CGRectGetMaxY(self.titleLable.frame) + 9, CGRectGetWidth(self.titleLable.frame),CGRectGetHeight(self.titleLable.frame))];
self.remarkLable.textColor = [UIColor grayColor];
_remarkLable.font = [UIFont systemFontOfSize:14];
self.remarkLable.numberOfLines = 0;
[self addSubview:_remarkLable];
// self.remarkLable.backgroundColor = [UIColor redColor];
self.timeLable = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.titleLable.frame),CGRectGetMaxY(self.remarkLable.frame) + 9, CGRectGetWidth(self.remarkLable.frame),CGRectGetHeight(self.titleLable.frame))];
self.timeLable.textColor = [UIColor lightGrayColor];
_timeLable.font = [UIFont systemFontOfSize:12];
[self addSubview:_timeLable];
self.flowStateImageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, (100 - 22)/2.0, 22, 22)];
_flowStateImageView.layer.cornerRadius = 11;
_flowStateImageView.clipsToBounds = YES;
[self addSubview:_flowStateImageView];
self.lineView = [[UIView alloc] initWithFrame:CGRectMake(50,100, kScreenWidth - 50 - 15, 0.5)];
_lineView.backgroundColor = [UIColor redColor];
[self addSubview:_lineView];
}
+(CGFloat)getHeightWithString:(NSString *)str size:(CGSize)size
{
CGSize newSize = [str boundingRectWithSize:size
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14.0],NSFontAttributeName, nil] context:nil].size;
return newSize.height;
}
-(void)setContentWithDictionary:(NSMutableDictionary *)dic
{
self.titleLable.text = [NSString stringWithFormat:@"%@ %@ %@",dic[@"OPERATORNAME"],dic[@"NODENAME"],dic[@"TYPENAME"]];
self.remarkLable.text = dic[@"REMARK"];
self.timeLable.text = [NSString stringWithFormat:@"%@ %@",dic[@"OPERATEDATE"],dic[@"OPERATETIME"]];
CGSize size = CGSizeMake(kScreenWidth - 50 - 15, kScreenHeight);
CGFloat newHeight = [CustomTableViewCell getHeightWithString:self.remarkLable.text size:size];
self.remarkLable.frame = CGRectMake(self.remarkLable.frame.origin.x, self.remarkLable.frame.origin.y, self.remarkLable.frame.size.width, newHeight);
CGRect timeFrame = self.timeLable.frame;
timeFrame.origin.y = CGRectGetMaxY(self.remarkLable.frame) + 9;
self.timeLable.frame = timeFrame;
}
-(void)drawRect:(CGRect)rect
{
CGRect lineFrame = self.lineView.frame;
lineFrame.origin.y = self.bounds.size.height - 0.5;
self.lineView.frame = lineFrame;
CGRect flowStateframe = self.flowStateImageView.frame;
flowStateframe.origin.y = (rect.size.height - 22)/2.0;
self.flowStateImageView.frame = flowStateframe;
UIView *littleLine = [[UIView alloc] initWithFrame:CGRectMake((50-0.5)/2.0, 0, 0.5, self.bounds.size.height)];
littleLine.backgroundColor = [UIColor redColor];
[self addSubview:littleLine];
[self bringSubviewToFront:self.flowStateImageView];
CGRect frame = littleLine.frame;
if ([self.reuseIdentifier isEqualToString:@"cellFirstIdentifier"]) {
frame.origin.y = self.bounds.size.height/2.0;
frame.size.height = self.bounds.size.height/2.0;
littleLine.frame = frame;
}else if ([self.reuseIdentifier isEqualToString:@"cellLastIdentifier"])
{
frame.size.height = self.bounds.size.height/2.0;
littleLine.frame = frame;
}
}
-(void)prepareForReuse
{
[super prepareForReuse];
// if (self.isLast != YES) {
// self.lineView.hidden = NO;
// }else
// {
// self.lineView.hidden = YES;
// }
//
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
cell在tableView中的使用(.m文件)
#import "customViewController.h"
#import "CustomTableViewCell.h"
@interface customViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@end
@implementation customViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor redColor];
[self reloadData];
self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.tableView];
}
-(void)reloadData
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"List.plist" ofType:nil];
self.dataArr = [NSMutableArray arrayWithContentsOfFile:filePath];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//使用不一樣的identifier來標誌cell
static NSString *cellIdentifier = @"cellIdentifier";
static NSString * cellFirstIdentifier = @"cellFirstIdentifier";
static NSString * cellLastIdentifier = @"cellLastIdentifier";
CustomTableViewCell *cell;
if (indexPath.row == 0) {
cell = [self.tableView dequeueReusableCellWithIdentifier:cellFirstIdentifier];
}else if(indexPath.row == self.dataArr.count - 1)
{
cell = [self.tableView dequeueReusableCellWithIdentifier:cellLastIdentifier];
}else
{
cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
}
if (!cell) {
if (indexPath.row == 0) {
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellFirstIdentifier];
}else if (indexPath.row == self.dataArr.count - 1)
{
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellLastIdentifier];
}else
{
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
}
NSMutableDictionary *dataDic = [self.dataArr objectAtIndex:indexPath.row];
if (indexPath.row == 0 ) {
cell.isFirst = YES;
}else if (indexPath.row == self.dataArr.count - 1)
{
cell.isLast = YES;
// cell.lineView.hidden = YES;
}
if([dataDic[@"TYPENAME"] isEqualToString:@"退回"])
{
cell.flowStateImageView.image = [UIImage imageNamed:@"拒絕.png"];
}else if(!dataDic[@"TYPENAME"])
{
cell.flowStateImageView.image = [UIImage imageNamed:@"當前.png"];
}else
{
cell.flowStateImageView.image = [UIImage imageNamed:@"贊成.png"];
}
[cell setContentWithDictionary:dataDic];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *dataDic = [self.dataArr objectAtIndex:indexPath.row];
CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width - 50 - 15, [UIScreen mainScreen].bounds.size.height);
CGFloat remarkLableHeight = [CustomTableViewCell getHeightWithString:dataDic[@"REMARK"] size:size];
return (remarkLableHeight + 100 - ( 100 - 13 - 9 - 9 - 13)/3.0);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end