CodingNet - Learning - 5

原创 2016年06月01日 17:02:12

接下来进入到注册页面,原以为表面实在简单的注册页面,原来充满智慧!!!看完真是大呼过瘾!


1.极好的一个UITableView第三方实现TPKeyboardAvoidingTableView,具备不会挡住键盘,点击header 和 footer会自动收起键盘

2.神奇的自定义Cell,具备回调block执行能力,

3.RAC响应式的编程,虽然这里用得并不多,只绑定了button是否可点击的信号

4.输入@可以自动弹出可选栏

5.一个神奇的标签栏

一个个来学习!!


1.

这个强大的TableView使用是十分简单,和原生区别不大:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"注册";
    self.myRegister = [[Register alloc] init];
    _captchaNeeded = NO;
    
    //    添加myTableView
    _myTableView = ({
        TPKeyboardAvoidingTableView *tableView = [[TPKeyboardAvoidingTableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        [tableView registerNib:[UINib nibWithNibName:kCellIdentifier_Input_OnlyText_Cell bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kCellIdentifier_Input_OnlyText_Cell];
        tableView.backgroundColor = kColorTableSectionBg;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:tableView];
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
        tableView;
    });
    
    self.myTableView.tableHeaderView = [self customHeaderView];
    self.myTableView.tableFooterView=[self customFooterView];
    [self configBottomView];
    if (self.navigationController.childViewControllers.count <= 1) {
        self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithBtnTitle:@"取消" target:self action:@selector(dismissSelf)];
    }
}

另外header和footer的添加方式非常优雅:

- (UIView *)customHeaderView{
    UIView *headerV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 0.15*kScreen_Height)];
    headerV.backgroundColor = [UIColor clearColor];
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 50)];
    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.font = [UIFont boldSystemFontOfSize:18];
    headerLabel.textColor = [UIColor colorWithHexString:@"0x222222"];
    headerLabel.textAlignment = NSTextAlignmentCenter;
    headerLabel.text = @"加入Coding,体验云端开发之美!";
    [headerLabel setCenter:headerV.center];
    [headerV addSubview:headerLabel];
    
    return headerV;
}

这里有个隐含的惊喜,有时候需要点击一些空白的地方实现收起键盘,这个时候我们很多情况下都用手势来实现,而这个tableview会直接帮你实现了局部的功能

你点击header和footer的时候会自动收起键盘


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Input_OnlyText_Cell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_Input_OnlyText_Cell forIndexPath:indexPath];
    
    __weak typeof(self) weakSelf = self;
    
    if (indexPath.row == 0) {
        cell.textField.keyboardType = UIKeyboardTypeEmailAddress;
        [cell configWithPlaceholder:@" 电子邮箱" andValue:self.myRegister.email];
        
        cell.textValueChangedBlock = ^(NSString *valueStr){
            weakSelf.inputTipsView.valueStr = valueStr;
            weakSelf.inputTipsView.active = YES;
            weakSelf.myRegister.email = valueStr;
        };
        
        cell.editDidEndBlock = ^(NSString *textStr){
            weakSelf.inputTipsView.active = NO;
        };
    }else if (indexPath.row == 1){
        [cell configWithPlaceholder:@" 个性后缀" andValue:self.myRegister.global_key];
        cell.textValueChangedBlock = ^(NSString *valueStr){
            weakSelf.myRegister.global_key = valueStr;
        };
    }else{
        cell.isCaptcha = YES;
        [cell configWithPlaceholder:@" 验证码" andValue:self.myRegister.j_captcha];
        cell.textValueChangedBlock = ^(NSString *valueStr){
            weakSelf.myRegister.j_captcha = valueStr;
        };
    }
    [tableView addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:kLoginPaddingLeftWidth];
    return cell;
}

下面马上解析这个自定义Cell的实现


2.

首先在Cell中的textFiled并没有用实现textFieldDelegate的方式,而是这样的方式:

    [self.usernameTextField addTarget:self action:@selector(usernameTextFieldChanged) forControlEvents:UIControlEventEditingChanged];
    [self.passwordTextField addTarget:self action:@selector(passwordTextFieldChanged) forControlEvents:UIControlEventEditingChanged];

而布局代码则是使用layoutsubView来实现:

- (void)layoutSubviews {
    [super layoutSubviews];
    self.backgroundColor = _isForLoginVC? [UIColor clearColor]: [UIColor whiteColor];
    self.textField.font = [UIFont systemFontOfSize:17];
    self.textField.textColor = _isForLoginVC? [UIColor whiteColor]: [UIColor colorWithHexString:@"0x222222"];
    if (!_lineView && _isForLoginVC) {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(kLoginPaddingLeftWidth, 43.5, kScreen_Width-2*kLoginPaddingLeftWidth, 0.5)];
        _lineView.backgroundColor = [UIColor colorWithHexString:@"0xffffff" andAlpha:0.5];
        [self.contentView addSubview:_lineView];
    }
    if (_isCaptcha) {
        [self.textField setWidth:(kScreen_Width - 2*kLoginPaddingLeftWidth) - (_isForLoginVC? 90 : 70)];
        _captchaView.hidden = NO;
        [self refreshCaptchaImage];
    }else{
        [self.textField setWidth:(kScreen_Width - 2*kLoginPaddingLeftWidth) - (_isForLoginVC? 20:0)];
        _captchaView.hidden = YES;
    }
    [self.clearBtn setX:self.textField.maxXOfFrame - 10];
    _lineView.hidden = !_isForLoginVC;
    _clearBtn.hidden = YES;
    self.textField.clearButtonMode = _isForLoginVC? UITextFieldViewModeNever: UITextFieldViewModeWhileEditing;

}


接下来我们看看如何用 target-Action 加 block实现一个完美的回调:

两个block变量:

@property (nonatomic,copy) void(^textValueChangedBlock)(NSString*);
@property (nonatomic,copy) void(^editDidEndBlock)(NSString*);


在Action上执行:

- (IBAction)textValueChanged:(id)sender {
    self.clearBtn.hidden = _isForLoginVC? self.textField.text.length <= 0: YES;
    if (self.textValueChangedBlock) {
        self.textValueChangedBlock(self.textField.text);
    }
}

- (IBAction)editDidEnd:(id)sender {
    _lineView.backgroundColor = [UIColor colorWithHexString:@"0xffffff" andAlpha:0.5];
    self.clearBtn.hidden = YES;
    if (self.editDidEndBlock) {
        self.editDidEndBlock(self.textField.text);
    }
}


在外层类上实现:

        cell.textValueChangedBlock = ^(NSString *valueStr){
            weakSelf.inputTipsView.valueStr = valueStr;
            weakSelf.inputTipsView.active = YES;
            weakSelf.myRegister.email = valueStr;
        };
        
        cell.editDidEndBlock = ^(NSString *textStr){
            weakSelf.inputTipsView.active = NO;
        };

实现了超级好看的代码格式!!!



3.

RAC强烈推荐一个学习的文章:

http://www.cocoachina.com/ios/20150123/10994.html   学习后应该没有什么问题

    RAC(self, footerBtn.enabled) = [RACSignal combineLatest:@[RACObserve(self, myRegister.email), RACObserve(self, myRegister.global_key), RACObserve(self, myRegister.j_captcha), RACObserve(self, captchaNeeded)] reduce:^id(NSString *email, NSString *global_key, NSString *j_captcha, NSNumber *captchaNeeded){
        
        if ((captchaNeeded && captchaNeeded.boolValue) && (!j_captcha || j_captcha.length <= 0)) {
            return @(NO);
        }else{
            return @((email && email.length > 0) && (global_key && global_key.length > 0));
        }
    }];



4.

CodingNet实现的这个也是非常棒的

利用重写了setValue的方法来实现联动:

- (void)setValueStr:(NSString *)valueStr{
    _valueStr = valueStr;
    if (_valueStr.length <= 0) {
        self.dataList = nil;
    }else if ([_valueStr rangeOfString:@"@"].location == NSNotFound) {
        self.dataList = _type == EaseInputTipsViewTypeLogin? [self loginList]: nil;
    }else{
        self.dataList = [self emailList];
    }
    [self refresh];
}


更妙的是这个block,和上面一样的回调原理:

@property (nonatomic, copy) void(^selectedStringBlock)(NSString *);

选择后实现回调执行:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.selectedStringBlock && self.dataList.count > indexPath.row) {
        self.selectedStringBlock([self.dataList objectAtIndex:indexPath.row]);
    }
}


创建该视图的时候就实现了这个回调执行内容:

    if (!_inputTipsView) {
        _inputTipsView = ({
            EaseInputTipsView *tipsView = [EaseInputTipsView tipsViewWithType:EaseInputTipsViewTypeRegister];
            tipsView.valueStr = nil;
            
            __weak typeof(self) weakSelf = self;
            
            tipsView.selectedStringBlock = ^(NSString *valueStr){
                
                [weakSelf.view endEditing:YES];
                weakSelf.myRegister.email = valueStr;
                [weakSelf.myTableView reloadData];
            
            };
            UITableViewCell *cell = [_myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
            [tipsView setY:CGRectGetMaxY(cell.frame)];
            
            [_myTableView addSubview:tipsView];
            tipsView;
        });
    }


5.

TTTAttributedLabel

这个神奇的标签栏就不多说了,很多标签和URL链接渲染都是由它实现,例如微信的朋友劝评论标签等等



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

CodingNet - Learning - 6

网络层封装

CodingNet - Learning - 10

朦胧的动画显示遮罩

CodingNet - Learning - 2

启动页动画

CodingNet - Learning - 3

介绍页实现

深度学习FPGA实现基础知识5(网友一致认可的----Deep Learning(深度学习)学习笔记整理及完整版下载)

需求说明:深度学习FPGA实现知识储备 来自:http://blog.csdn.net/zouxy09/article/details/8775360/ Deep Learning(深度学习)...

周志华《Machine Learning》学习笔记(5)--决策树

上篇主要介绍和讨论了线性模型。首先从最简单的最小二乘法开始,讨论输入属性有一个和多个的情形,接着通过广义线性模型延伸开来,将预测连续值的回归问题转化为分类问题,从而引入了对数几率回归,最后线性判别分析...

Neural Networks and Deep Learning学习笔记ch5 - 为什么深度神经网络很难训练?

深度神经网络前面的章节介绍了反向传播算法和一些常见的改进神经网络的训练效果的方法。前面还只是停留在只有一个隐藏层的神经网络。 如上图所示,这样的一个神经网络就只有输入、输出层和一个隐藏层。在手写...

<纯干货-5>Deep Reinforcement Learning深度强化学习_论文大集合

本文罗列了最近放出来的关于深度强化学习(Deep Reinforcement Learning,DRL)的一些论文。文章采用人工定义的方式来进行组织,按照时间的先后进行排序,越新的论文,排在越前面。希...

Coursera Machine Learning 第六周 Programming Exercise 5: Regularized Linear Regression and Bias

linearRegCostFunction.m function [J, grad] = linearRegCostFunction(X, y, theta, lambda) %LINEARREGCO...

深度学习deep learning环境配置:GTX1080+CUDA8.0+cudnn5

GTX1080http://blog.csdn.net/v_july_v/article/details/52658965 http://blog.csdn.net/u010900574/artic...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:CodingNet - Learning - 5
举报原因:
原因补充:

(最多只允许输入30个字)