快件跟踪记录对【】内容进行标红

效果图如下 

 

实现此功能   肯定是用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;

}

这样操作的好处是  不需要对每一个属性进行单独的赋值  只需要传入一个实体类就够了  而且代码也不乱  看起来清爽

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值