ios开发整理

iOS 纯代码自定义UITableview,对cell里有tex

2017-04-01  本文已影响365人  丶逝水流年

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

文章里对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;

}

上一篇下一篇

猜你喜欢

热点阅读