效果图如下
实现此功能 肯定是用UITableView 然后自定制Cell 并对自定制的Cell进行包装 实现实体类赋值
新建自定制Cell .h如下 对于一些功能都写上了注释
//
// TrackHistoryCell.h
// Frame3.0
//
// Created by lxy on 2018/12/26.
// Copyright © 2018 Shenzhen MSD Technology Co.,LTD. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "KuaiJianBean.h"
@interface TrackHistoryCell : UITableViewCell
/**
传入一个实体类 model
*/
@property (nonatomic,strong)KuaiJianBean *bean;
/**
对实体类进行赋值
@param bean 传入的实体类
*/
- (void)setDataWithBean:(KuaiJianBean *)bean;
@end
.m文件如下 赋值操作方法中 有【】内容标红方法
//
// TrackHistoryCell.m
// Frame3.0
//
// Created by lxy on 2018/12/26.
// Copyright © 2018 Shenzhen MSD Technology Co.,LTD. All rights reserved.
//
#import "TrackHistoryCell.h"
@interface TrackHistoryCell()
/**
Cell的属性
*/
@property (nonatomic,strong)UILabel *trackLabel;
@property (nonatomic,strong)UILabel *timeLabel;
@property (nonatomic,strong)UIView *bgView;
@end
@implementation TrackHistoryCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self){
[self initUI];
}
return self;
}
/**
初始化CellUI
*/
- (void)initUI{
_bgView = [[UIView alloc]init];
_bgView.backgroundColor = [UIColor whiteColor];
_bgView.frame = lxy(0, 0, SCREEN_WIDTH-20, 90);
[self.contentView addSubview:_bgView];
_timeLabel = [[UILabel alloc]init];
_timeLabel.font = [UIFont boldSystemFontOfSize:15];
_timeLabel.frame = lxy(10, 5, _bgView.width-20, 20);
[_bgView addSubview:_timeLabel];
_trackLabel = [[UILabel alloc]init];
_trackLabel.font = smallFont;
_trackLabel.frame = lxy(_timeLabel.left, _timeLabel.bottom+5, _timeLabel.width, 20);
_trackLabel.numberOfLines = 0;
[_bgView addSubview:_trackLabel];
}
/**
赋值操作
*/
- (void)setDataWithBean:(KuaiJianBean *)bean{
_timeLabel.text = bean.scantime;
_trackLabel.text = bean.desc;
CGSize labelSize = [bean.desc boundingRectWithSize:CGSizeMake(_trackLabel.width, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:_trackLabel.font forKey:NSFontAttributeName] context:nil].size;
_trackLabel.height = labelSize.height;
_bgView.height = _trackLabel.height+10;
/**
对【】中的关键字进行标红 try catch防止异常情况
*/
@try{
NSMutableAttributedString *hintString=[[NSMutableAttributedString alloc]initWithString:_trackLabel.text];
NSArray * arr1 = [[hintString string]componentsSeparatedByString:@"【"];
NSArray * arr2 = [[hintString string]componentsSeparatedByString:@"】"];
NSInteger loc = 0;
NSInteger len = 0;
for (int i=0; i<arr1.count-1; i++){
if (i>=1) {
loc += [arr1[i] length];
len += [arr2[i] length];
}else if(i==0){
loc = [arr1[i] length];
len = [arr2[i] length];
}
[hintString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(loc+i+1, len-loc-1)];
}
_trackLabel.attributedText = hintString;
} @catch (NSException *exception){
}@finally{
}
}
@end
UITableView赋值操作如下
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *str = @"Cell";
TrackHistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
if (!cell){
cell = [[TrackHistoryCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
KuaiJianBean *bean = [[KuaiJianBean alloc]init];
//进行赋值 并使得跟踪记录是逆序的 即最新日期的记录在最上面显示
[bean mj_setKeyValues:self.dataArray[self.dataArray.count-1- indexPath.section]];
[cell setDataWithBean:bean];
return cell;
}
这样操作的好处是 不需要对每一个属性进行单独的赋值 只需要传入一个实体类就够了 而且代码也不乱 看起来清爽