之前产品有个需求,让画一个cell长这样:
![](https://img.haomeiwen.com/i1913758/e345358fd220467a.png)
现在说,有些地方的cell在输入框右边个按钮,可以通过拍照识别一些信息再回显到输入框。设计长这样:
![](https://img.haomeiwen.com/i1913758/a6b5e1773f44b039.png)
遇到这种前人已经写好了,自己又懒的看别人写的代码的情况,又要完成领导交给的任务。最好的方法就是继承上面那个。在子类中微调来实现。
分析父类的情况:
- 基础组件的布局(label,button,textfield等)
- 数据绑定 cell 上绑定数据。
@interface MDTableViewCell()
{
UITextField *_textFeild;
UILabel *_nameLabel;
UIImageView *_imageView;
}
@end
@implementation MDTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UITextField *textFeild = [[UITextField alloc]init];
textFeild.backgroundColor = [UIColor grayColor];
_textFeild = textFeild;
[self addSubview:textFeild];
UILabel *nameLabel = [[UILabel alloc]init];
_nameLabel = nameLabel;
[self addSubview: nameLabel];
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"avatar_white_load_view_bg"]];
_imageView = imageView;
[self addSubview:imageView];
[nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.mas_top);
make.left.mas_equalTo(self.mas_left).offset(15);
make.bottom.mas_equalTo(self.mas_bottom);
make.width.mas_equalTo(@(40));
}];
[imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(nameLabel.mas_top).offset(5);
make.bottom.mas_equalTo(nameLabel.mas_bottom).offset(-5);
make.left.mas_equalTo(nameLabel.mas_right).offset(10);
make.width.equalTo(@(40));
}];
[textFeild mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.mas_top);
make.right.mas_equalTo(self.mas_right).offset(-30);
make.left.mas_equalTo(imageView.mas_right).offset(30);
make.bottom.mas_equalTo(self.mas_bottom);
}];
}
return self;
}
设计子类,只需要重写下这个输入框,并且数据绑定正常即可。
@interface MDSubCell()
@property(nonatomic,strong)MDTextField *textField;
@end
@implementation MDSubCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {// 移除父类的textFiled,一定要移除掉父类的 TextField,只是子类赋值给父类对象,不会去重新渲染
UITextField *supersTextField = [self valueForKey:@"_textFeild"];// 若设计为公有属性,就可以直接访问到父类的属性,子类重写父类属性。但需要手动@synthesize 不然访问默认父类对象
if (supersTextField) {
[supersTextField removeFromSuperview];
}
// 子类新添加 MDTextField 为带有按钮的Textfield
self.textField = [[MDTextField alloc]init];
self.textField.borderStyle = UITextBorderStyleBezel;
[self addSubview:self.textField];
// 重新布局 其他的也可一并重新布局
UILabel *nameLabel = [self valueForKey:@"_nameLabel"];
[nameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.mas_left).offset(15);
}];
[_textField mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.mas_top);
make.right.mas_equalTo(self.mas_right).offset(-15);
make.left.mas_equalTo(self.mas_left).offset(130);
make.bottom.mas_equalTo(self.mas_bottom);
}];
}
return self;
}
@end
如果不需要访问组件的action,一般不会暴露组建在外面,所以只好通过KVC去访问了。如果是暴露出来的设计,那需要手动@synthesize 要重写的对象 不然访问默认父类对象。
网友评论