一起养成写作习气!这是我参与「日新方案 4 月更文应战」的第22天,点击查看活动概况。

前言

视图的组成部分为:

  • 标题
  • 修改区域
  • 字符约束:iOS文本长度计算规则:中文占1,英文等能转ascii的占0.5

iOS小技能: 封装textView 为一个cell,用于备注信息编辑/街道地区信息的填写。

完成思路:自定义cell

应用场景:用于备注信息修改/街道区域信息的填写。

I 用法

  • // 修改具体区域视图模型
// 修改具体区域视图模型
    self.viewModel.delivery.tvModel =  [QCTTextViewModel new];
    //        //
    self.viewModel.delivery.tvModel.Labtitle = QCTLocal(@"ERP_Detailed_area");
    self.viewModel.delivery.tvModel.wordMaxCount = 200;
    self.viewModel.delivery.tvModel.titlePlaceholder = QCTLocal(@"ERP_Street_no");
//
//
    self.viewModel.delivery.tvModel.placeholderLabfont =  kTextFont(14);
    self.viewModel.delivery.tvModel.placeholderLabtextColor =  rgb(198,198,200);
//
  • 创立一个分组
    /**
     具体区域
     */
    ERPChageAddreeVCViewSectionType4Reason,
  • 分组个数1
        case ERPChageAddreeVCViewSectionType4Reason:{
            return 1;
        }
            break;
  • 创立 cell
            case ERPChageAddreeVCViewSectionType4Reason:
            {
                ERPQCTMarkAddress200TableViewCell *cell = [ERPQCTMarkAddress200TableViewCell tableViewCellWithTableView:tableView block:^(id  _Nonnull sender) {
                } models:self.viewModel.delivery];
                return cell;
            }
                break;

II 完成

思路:自定义cell

2.1 cell完成

头文件

#import <UIKit/UIKit.h>
#import "ERPQCTMarkAddress200TableViewCellView.h"
NS_ASSUME_NONNULL_BEGIN
@interface ERPQCTMarkAddress200TableViewCell : UITableViewCell
@property (nonatomic, strong) id models;
@property (nonatomic, copy) void (^block)(id sender);
+ (instancetype) tableViewCellWithTableView:(UITableView *) tableView   block:(void (^)(id sender))block  models:(id) models ;
+ (instancetype) tableViewCellWithTableView:(UITableView *) tableView;
@property (nonatomic,weak) ERPQCTMarkAddress200TableViewCellView *cellView;

完成文件


#import "ERPQCTMarkAddress200TableViewCell.h"
@implementation ERPQCTMarkAddress200TableViewCell
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;//去掉选中作用
        [self selfInit];
        [self createSubView];
        [self bindViewModel];
    }
    return self;
}
- (void)bindViewModel {
}
- (void)selfInit{
    self.contentView.backgroundColor = kcellColor;
}
- (void)createSubView {
    [self cellView];
}
static NSString *identifier = @"ERPQCTMarkAddress200TableViewCell";//QCTCheckOutViewcellSection4topTableViewCell
+ (instancetype) tableViewCellWithTableView:(UITableView *) tableView{
    ERPQCTMarkAddress200TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {// ------并不会履行
        cell = [[ERPQCTMarkAddress200TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
}
+ (instancetype) tableViewCellWithTableView:(UITableView *) tableView   block:(void (^)(id sender))block  models:(id) models {
    ERPQCTMarkAddress200TableViewCell *cell  = [self  tableViewCellWithTableView:tableView];
    if (block) {
        cell.block = block;
    }
    cell.models = models;
    return cell;
}
#pragma mark - ******** model
- (void)setModels:( id)models{
    _models =models;
        self.cellView.models = models;
}
- (ERPQCTMarkAddress200TableViewCellView *)cellView{
    if (nil == _cellView) {
        ERPQCTMarkAddress200TableViewCellView *tmpView = [[ERPQCTMarkAddress200TableViewCellView alloc]init];
        _cellView = tmpView;
        [tmpView setBackgroundColor:kcellColor];
        [self.contentView addSubview:_cellView];
        __weak __typeof__(self) weakSelf = self;
        [_cellView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.contentView).offset(kAdjustRatio(25));
            make.right.equalTo(weakSelf.contentView).offset(- kAdjustRatio(25));
            make.top.equalTo(weakSelf.contentView).offset(kAdjustRatio(0));
            make.bottom.equalTo(weakSelf.contentView).offset(kAdjustRatio(0));
        }];
        [_cellView setBlock:^(id  _Nonnull sender) {
            if(weakSelf.block){
                weakSelf.block(sender);
            }
        }];
    }
    return _cellView;
}
@end

2.2 cellV的完成

ERPQCTMarkAddress200TableViewCellView.h


#import <UIKit/UIKit.h>
#import "QCTERPOrderDeliveryDto.h"
NS_ASSUME_NONNULL_BEGIN
@interface ERPQCTMarkAddress200TableViewCellView : UIView
@property (nonatomic, strong) QCTERPOrderDeliveryDto* models;
@property (nonatomic, copy) void (^block)(id sender);
//view
@property (nonatomic,weak) UILabel *titleLab;
@property (nonatomic,weak) QCTContentView *contentView;
@property (strong, nonatomic) QCTTextViewModel *tvModel;
@end
NS_ASSUME_NONNULL_END

完成

//
//  ERPQCTMarkAddress200TableViewCellView.m
//  retail
//
//  Created by mac on 2020/2/20.
//  Copyright  2020 QCT. All rights reserved.
//
#import "ERPQCTMarkAddress200TableViewCellView.h"
@implementation ERPQCTMarkAddress200TableViewCellView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (QCTTextViewModel *)tvModel{
    if (nil == _tvModel) {
        QCTTextViewModel *tmpView = [[QCTTextViewModel alloc]init];
        _tvModel = tmpView;
    }
    return _tvModel;
}
//- (QCTERPOrderDeliveryDto *)delivery{
- (void)setModels:(QCTERPOrderDeliveryDto *)models{
    _models = models;
    if(![NSStringQCTtoll isBlankString:models.address] ){
        self.tvModel.content = models.address;
        if(models.tvModel){
            models.tvModel.content = models.address;
        }
    }
    if(models.tvModel){
//        models.tvModel.content = models.address;
        self.tvModel = models.tvModel;
        if(![NSStringQCTtoll isBlankString: models.tvModel.Labtitle]){
            _titleLab.text = models.tvModel.Labtitle;
//            QCTLocal(@"ERP_Detailed_area");
        }
    }
    //"ERP_Detailed_area"="具体区域";
    [self.contentView updateModel:self.tvModel];
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self titleLab];
        [self contentView];
        // 默认设置
        self.tvModel.wordMaxCount = 200;
        self.tvModel.titlePlaceholder = QCTLocal(@"store_return_goods_choose_to_fill_in_limited_200_words");
        self.tvModel.placeholderLabfont =  kTextFont(14);
        self.tvModel.placeholderLabtextColor = rgb(204,204,204);
        [self.contentView updateModel:self.tvModel];
    }
    return self;
}
- (UILabel *)titleLab{
    if (!_titleLab) {
        UILabel *tmp = [[UILabel alloc]init];
        _titleLab = tmp;
        _titleLab.font = kPingFangFont(15);
        _titleLab.textAlignment = NSTextAlignmentLeft;
        //"ERP_Detailed_area"="具体区域";
        _titleLab.text = QCTLocal(@"store_return_goods_remarks");
        _titleLab.textColor = rgb(51,51,51);
        [self addSubview:_titleLab];
        __weak __typeof__(self) weakSelf = self;
        [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf).offset(kAdjustRatio(0));//18
            make.top.equalTo(weakSelf).offset(kAdjustRatio(10));
        }];
    }
    return _titleLab;
}
- (UIView*)contentView{
    if (!_contentView) {
        QCTContentView *tmp = [QCTContentView new];
        _contentView = tmp;
        [self addSubview:_contentView];
        __weak __typeof__(self) weakSelf = self;
        [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.titleLab.mas_bottom).offset(kAdjustRatio(0));
            make.left.bottom.right.equalTo(weakSelf);
            make.height.mas_equalTo(kAdjustRatio(150));
        }];
        _contentView.block = ^(id  _Nonnull sender) {
            NSLog(@"具体地址:%@",sender);
            if(weakSelf.block){
                weakSelf.block(sender);
            }
            //QCTeditReturnedItemTableViewCellModel
            weakSelf.models.address = sender;
        };
    }
    return _contentView;
}
@end

III 处理UITextView的占位符

监听文本的变化

- (void)textViewDidChange:(UITextView *)textView{
        if (textView.text.length == 0) {
            self.placeholderLab.hidden = NO;
        }else{
            self.placeholderLab.hidden = YES;
        }
}

监听删去键

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if (![text isEqualToString:@""]){
        _placeholderLab.hidden = YES;
    }
   else  if (([text isEqualToString:@""] && range.location == 0 && range.length == 1) || ([text isEqualToString:@"\n"] && [text isEqualToString:@""] && range.location == 0 && range.length == 1)){
        _placeholderLab.hidden = NO;
    }
   else  if ([text isEqualToString:@"\n"]){
        [textView endEditing:YES];
        return NO;
    }
        return YES;
}

see also

公众号:iOS逆向