iOS 纯代码自定义UITableview,对cell里有text文本的处理

本人刚接触纯代码布局,所以请大神不要嘲笑,写个文章记录一下,文章里用到了一个把图片设置成圆形的分类,如有需要请留言!

文章里对text文本的处理,通用于collectionview。欢迎大家一起交流,学习。

群号:310800319

群号:246807516

步骤:

1.自定义tableviewCell类

2.模型类

3. 在一个UIViewController里添加一个uitableview

第一步:自定义tableviewCell类

tableviewCell.h里:

#import@class PersonalInformationEditorModel;

@interface PersonalInformationEditorCell : UITableViewCell

/** model */

@property(nonatomic,strong)PersonalInformationEditorModel *model;

@property(nonatomic,strong) UILabel  *labelName;

/** wenben  */

@property(nonatomic,strong)UITextField *attribute;

@end

tableviewCell.m里:

#import "PersonalInformationEditorCell.h"

#import "PersonalInformationEditorModel.h"

@interface PersonalInformationEditorCell ()

@end

@implementation PersonalInformationEditorCell

- (void)awakeFromNib {

[super awakeFromNib];

}

//通过纯代码的方式创建此类实例 会调用该方法  在此方法中,添加子控件

- (instancetype)initWithFrame:(CGRect)frame{

if(self = [super initWithFrame:frame]){

//选中cell的图片

UILabel *label = [[UILabel alloc]init];

[self addSubview:label];

self.labelName = label;

}

return self;

}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

if (self) {

//创建自定义的视图

[self creatView];

}

return self;

}

#pragma mark 设置子控件

- (void)creatView{

[self addSubview:self.labelName];

[self.labelName mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.equalTo(20);

make.left.equalTo(20);

}];

[self addSubview:self.attribute];

[self.attribute mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.equalTo(20);

make.right.equalTo(-20);

make.width.equalTo(200);

}];

}

#pragma maik 懒加载

-(UILabel *)labelName{

if (_labelName == nil) {

_labelName = [[UILabel alloc]init];

}

return _labelName;

}

-(UITextField *)attribute{

if (_attribute == nil) {

_attribute = [[UITextField alloc]init];

_attribute.borderStyle = UITextBorderStyleNone;

_attribute.placeholder = @"请输入要修改的内容";

_attribute.clearButtonMode = UITextFieldViewModeWhileEditing;

_attribute.textAlignment = UITextAlignmentRight;

_attribute.keyboardType = UIKeyboardTypeDefault;

_attribute.returnKeyType =UIReturnKeyDone;

}

return _attribute;

}

//设置子控件的内容

-(void)setModel:(PersonalInformationEditorModel *)model{

_model = model;

[self.labelName setText:model.name] ;

[self.attribute setText:model.attribute];

}

@end

第二步:模型类

模型类.h里:

#import@interface PersonalInformationEditorModel : NSObject

//名称

@property(nonatomic,copy)NSString *name;

//属性

@property(nonatomic,copy)NSString *attribute;

//快速创建实例

+ (instancetype)appWithDict:(NSDictionary *)dict;

@end

模型类l.m里:

#import "PersonalInformationEditorModel.h"

@implementation PersonalInformationEditorModel

+ (instancetype)appWithDict:(NSDictionary *)dict{

//这样写会通用 可以把这个 作为代码块了

id obj = [[self alloc] init];

[obj setValuesForKeysWithDictionary:dict];

return obj;

}

@end

第三步:在一个UIViewController里添加一个UITableview

UITableview.h里没有代码,所以直接.m  ------UITableview.m里:

#import "PersonalInformationEditorVC.h"#import "UIImage+BSExtension.h"#import "PersonalInformationEditorCell.h"#import "PersonalInformationEditorModel.h"static NSString *ID = @"PersonalInformationEditorCell";@interface PersonalInformationEditorVC ()/** tableivew */

@property(nonatomic,strong)UITableView *tableView;

/** 数组 */

@property(nonatomic,strong)NSMutableArray *models;

@end

@implementation PersonalInformationEditorVC

- (void)viewDidLoad {

[super viewDidLoad];

[self setupUI];

[self setupNavigation];

[self setupTableView];

[self setupKeyFrame];

self.view.backgroundColor = [UIColor whiteColor];

}

- (void)setupUI {

//顶部头像view

UIView *topView = [[UIView alloc]init];

[self.view addSubview:topView];

//    topView.backgroundColor = [UIColor orangeColor];

[topView mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.left.right.equalTo(0);

make.top.height.equalTo(200);

}];

//头像

UIImageView *headImage = [[UIImageView alloc]initWithImage:[UIImage circleImage:@"user2"]];

[topView addSubview:headImage];

[headImage mas_makeConstraints:^(MASConstraintMaker *make) {

make.centerX.equalTo(topView.centerX);

make.size.equalTo(CGSizeMake(100, 100));

make.top.equalTo(50);

}];

//名字

UILabel *nameLable = [[UILabel alloc]init];

nameLable.text = @"王宝强";

[topView addSubview:nameLable];

[nameLable mas_makeConstraints:^(MASConstraintMaker *make) {

make.centerX.equalTo(topView.centerX);

make.top.equalTo(headImage.bottom).offset(20);

}];

//tableview

[self.view addSubview:self.tableView];

[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.equalTo(topView.bottom);

make.left.right.equalTo(0);

make.height.equalTo(self.view.height).offset(-200);

}];

}

-(void)setupKeyFrame{

//新增监听,当键盘出现

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hotKeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

//新增监听,当键盘退出

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hotKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

UITapGestureRecognizer *topGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(keyboardHide:)];

//设置成NO表示当前控件响应后会传播到其他控件上,默认yes

topGestureRecognizer.cancelsTouchesInView = NO;

//讲触摸事件添加到当前view

[self.view addGestureRecognizer:topGestureRecognizer];

}

- (void)setupNavigation {

self.title = @"个人信息编辑";

self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [CCTools stringToColor:@"#333333"]};

}

-(void)viewWillAppear:(BOOL)animated{

[super viewWillAppear:animated];

[self.navigationController setNavigationBarHidden:NO];

}

-(void)setupTableView{

//注册cell

[self.tableView registerClass:[PersonalInformationEditorCell class] forCellReuseIdentifier:ID];

}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return self.models.count;

}

//设置单元格的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPat{

//这里设置cell的高度

return 60;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

PersonalInformationEditorCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

//给cell设置数据

cell.model = self.models[indexPath.row];

//处理键盘

[cell.attribute addTarget:self action:@selector(messageFiedClick:) forControlEvents:UIControlEventEditingChanged];

cell.attribute.delegate = self;

return cell;

}

#pragma -mark 代码方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

//取消选中  点击之后手放开就选中效果消失再执行下面的代码

[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

#pragma mark -监听文本输入值

-(void)messageFiedClick:(UITextField *)textfield{

DLog(@"监听文本输入值");

}

#pragma mark -  键盘处理    回收键盘

- (void)keyboardHide:(UITapGestureRecognizer *)tap{

DLog(@"页面停止编辑");

[self.view endEditing:YES]; //页面停止编辑

}

#pragma mark 键盘出现

- (void)hotKeyboardWillShow:(NSNotification *)aNotification{

DLog(@" 键盘出现");

//取得键盘最后得frame

CGRect keyBoardRect=[aNotification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

//获取键盘高度

//  CGFloat height = keyBoardRect.size.height;

[UIView animateWithDuration:0.2 animations:^{

//修改的view的属性放在这里block中

self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyBoardRect.size.height, 0);

} completion:^(BOOL finished) {

}];

}

#pragma mark 键盘消失

- (void)hotKeyboardWillHide:(NSNotification *)aNotification{

DLog(@" 键盘消失");

//收键盘的时候按钮回去

[UIView animateWithDuration:0.2 animations:^{

//修改的view的属性放在这里block中

self.tableView.contentInset =UIEdgeInsetsZero;

} completion:^(BOOL finished) {

}];

}

#pragma mark - 完成 点击事件

- (BOOL)textFieldShouldReturn:(UITextView *)textField{

[textField resignFirstResponder];

return YES;

}

-(void)dealloc{

[[NSNotificationCenter defaultCenter]removeObserver:self];

}

#pragma mark -懒加载

-(NSMutableArray *)models{

if (_models == nil) {

//获得app.plist的路径

NSString *path = [[NSBundle mainBundle] pathForResource:@"SetPersonalInformationEditor" ofType:@"plist"];

//加载app.plist中的字典为数组

NSArray *arr = [NSArray arrayWithContentsOfFile:path];

NSMutableArray *array = [NSMutableArray array];

//字典转模型

for(NSDictionary *dict in arr){

PersonalInformationEditorModel *model = [PersonalInformationEditorModel appWithDict:dict];

[array addObject:model];

}

_models = array;

}

return _models;

}

-(UITableView *)tableView{

if (_tableView == nil) {

_tableView = [[UITableView alloc]init];

_tableView.delegate = self;

_tableView.dataSource = self;

self.tableView.tableFooterView = [[UIView alloc] init];//隐藏多余的分割线

}

return _tableView;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容