CodingNet - Learning - 18

来源:互联网 发布:7405单片机管脚图 编辑:程序博客网 时间:2024/05/19 02:30

接下来就进入这个页面内比较好玩的一个Cell:TaskContentCell

@property (nonatomic, copy) void(^textValueChangedBlock)(NSString *);@property (nonatomic, copy) void(^textViewBecomeFirstResponderBlock)();@property (nonatomic, copy) void(^deleteBtnClickedBlock)(Task *);@property (nonatomic, copy) void(^descriptionBtnClickedBlock)(Task *);@property (nonatomic, copy) void(^addTagBlock)();@property (nonatomic, copy) void(^tagsChangedBlock)();

可以看到定义了很多个block,这些block都是为了回调,举出两处用的很好的地方,结合了UITextView原生的Delegate使用。

#pragma mark TextView Delegate- (void)textViewDidChange:(UITextView *)textView{    if (self.textValueChangedBlock) {        self.textValueChangedBlock(textView.text);    }}- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{    if (self.textViewBecomeFirstResponderBlock) {        self.textViewBecomeFirstResponderBlock();    }    return YES;}

可以看到,利用ShouldBeginEditing就可以用得非常好,可以达到will Do的效果,非常棒,而回调处:

cell.textValueChangedBlock = ^(NSString *textStr){    weakSelf.myCopyTask.content = textStr;};cell.textViewBecomeFirstResponderBlock = ^(){    [weakSelf.myMsgInputView isAndResignFirstResponder];    [weakSelf.myTableView setContentOffset:CGPointZero animated:YES];};


整个写法简洁,明了


绿色的 “添加标签” 自定义View和动作都需要单独研究,我们看到layoutSubViews的实现:

- (void)layoutSubviews{    [super layoutSubviews];    if (!_task) {        return;    }    _tagsView.tags = _task.labels;    [_tagsView mas_remakeConstraints:^(MASConstraintMaker *make) {        make.top.equalTo(self.contentView).offset(10);        make.left.right.equalTo(self.contentView);        make.height.mas_equalTo([ProjectTagsView getHeightForTags:self.task.labels]);    }];        _taskContentView.text = _task.content;        if (_task.handleType > TaskHandleTypeEdit) {        _creatorLabel.text = [NSString stringWithFormat:@"%@ 现在", _task.creator.name];        _deleteBtn.hidden = YES;        _numLabel.hidden = YES;    }else{        _numLabel.text = [NSString stringWithFormat:@"#%@  ", _task.number.stringValue];        _creatorLabel.text = [NSString stringWithFormat:@"%@ 创建于 %@", _task.creator.name, [_task.created_at stringDisplay_HHmm]];        _deleteBtn.hidden = !([_task.creator.global_key isEqualToString:[Login curLoginUser].global_key] || _task.project.owner_id.integerValue == [Login curLoginUser].id.integerValue);    }}


可以知道,标签内容是用laytouSubViews来及时刷新的


并且注意到上面,mas_remakeConstraints ,则会清除之前的所有约束 仅保留最新的约束,这个约束更新也需要掌握好



这里我们试着点击“添加标签”的这一整行,可以发现,整一行都可以响应点击的事件,我们的常识是放一个UIButton进去,咦,这样也太奇妙了点吧!接下来我们就研究这个挺好玩的自定义标签:
这个自定义的标签,我已经抽取出来了,整个实现和解析都放在Github上,可以直接拿去当做控件使用:


https://github.com/iosfighterlb/TagMarks

0 0