关闭

CodingNet - Learning - 5

195人阅读 评论(0) 收藏 举报
分类:

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


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链接渲染都是由它实现,例如微信的朋友劝评论标签等等



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:74059次
    • 积分:4321
    • 等级:
    • 排名:第7268名
    • 原创:366篇
    • 转载:7篇
    • 译文:4篇
    • 评论:3条
    最新评论