美文网首页111
iOS 超简单1种cell高度自定义实现个人设置 &&a

iOS 超简单1种cell高度自定义实现个人设置 &&a

作者: 翻滚的炒勺2013 | 来源:发表于2018-01-26 16:14 被阅读123次
    timg.jpg

    先看效果

    Untitled.gif

    1.mvc思想封装一个常见样式的cell,cell的具体展示由模型决定

    2.保证左端无论有没有 *号都会保持对齐,右侧也是一样,无论是否有图片或者UISwitch都会对齐到最右端

    3.目前取出点击头像的事件,UISwitch的切换事件

    4.持续更新

    控制器代码

    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self setupUI];
        [self creatMData];
        self.navigationItem.title = @"个人设置";
    }
    
    - (void)setupUI {
        [self.view addSubview:self.tableView];
    }
    - (void)creatMData {
        GHFromModel *fromModel = [[GHFromModel alloc]init];
        self.dataArray = [fromModel creatFromMData];
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return self.dataArray.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
        GHFromCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GHFromCell"];
        cell.fromModel = self.dataArray [indexPath.row];
        return cell;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        GHFromModel *fromModel =  self.dataArray [indexPath.row];
        return fromModel.cellHeight;
    }
    - (NSMutableArray *)dataArray {
        if (_dataArray == nil) {
            _dataArray = [NSMutableArray array];
        }
        return _dataArray;
    }
    - (UITableView *)tableView {
        if (_tableView == nil) {
            _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
            _tableView.delegate = self;
            _tableView.dataSource = self;
            [_tableView registerClass:[GHFromCell class] forCellReuseIdentifier:@"GHFromCell"];
            _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        }
        return _tableView;
    }
    

    cell代码

    
    - (void)setFromModel:(GHFromModel *)fromModel {
        _fromModel = fromModel;
        self.leftTitle.text = fromModel.leftTitle;
        self.rightDetails.placeholder = fromModel.placeholder;
        self.rightDetails.imageName = fromModel.imageName;
        self.leftTitle.isShowStar = fromModel.isShowStar;
        
        if (fromModel.cellType == GHFromCellType_sex) {
            self.rightDetails.isSwitch = YES;
    
        }
    }
    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
        if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
            [self setupUI];
            self.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return self;
    }
    
    - (void)setupUI {
        [self addSubview:self.leftTitle];
        [self addSubview:self.rightDetails];
        [self addSubview:self.line];
    
    }
    - (void)textField:(GHTextField *)textField image:(UIImage *)image{
        NSLog(@"%@",image);
    }
    
    - (void)textField:(GHTextField *)textField state:(BOOL)state {
        
        NSLog(@"state%d",state);
    
    }
    - (UIView *)line {
        if (_line == nil) {
            _line = [[UIView alloc]initWithFrame:CGRectMake(15, self.frame.size.height, [UIScreen mainScreen].bounds.size.width - 30, 0.5)];
            _line.backgroundColor = [UIColor lightGrayColor];
        }
        return _line;
    }
    - (GHTextField *)rightDetails {
        if (_rightDetails == nil) {
            _rightDetails = [[GHTextField alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 15 - 140, (self.frame.size.height - 30) *0.5, 140, 30)];
            _rightDetails.textAlignment = NSTextAlignmentRight;
            _rightDetails.textFieldDelegate = self;
        }
        return _rightDetails;
    }
    
    #pragma mark - TextField
    - (GHTextField *)leftTitle {
        if (_leftTitle == nil) {
            _leftTitle = [[GHTextField alloc]initWithFrame:CGRectMake(15, (self.frame.size.height - 30 ) * 0.5, 120, 30 )];
            _leftTitle.isShowStar = YES;
            _leftTitle.enabled = NO;
            _leftTitle.textColor = [UIColor darkGrayColor];
        }
        return _leftTitle;
    }
    
    
    

    构造json

    - (NSMutableArray *)creatFromMData {
        NSMutableArray *dataArray = [NSMutableArray array];
        NSArray *cellTypeNumArray =@[
                                /** 头像 */
                                @(GHFromCellType_icon),
                                /** 姓名 */
                                @(GHFromCellType_name),
                                /** 性别 */
                                @(GHFromCellType_sex),
                                /** 电话号码 */
                                @(GHFromCellType_phone),
                                /** 备注 */
                                @(GHFromCellType_content),
                                ];
        NSArray *leftTitleArray = @[
                                    @"头像",
                                    @"姓名",
                                    @"性别",
                                    @"手机号码",
                                    @"备注",
                                    ];
        NSArray *placeholderArray = @[
                                    @"",
                                    @"请输入姓名",
                                    @"",
                                    @"请选择手机号码",
                                    @"请输入备注",
                                    ];
        NSArray *imageNameArray = @[
                                    @"daikan",
                                    @"setting_arrow",
                                    @"",
                                    @"",
                                    @"",
                                    ];
        NSArray *isShowStartArray = @[
                                      [NSNumber numberWithBool:YES],
                                      [NSNumber numberWithBool:YES],
                                      [NSNumber numberWithBool:YES],
                                      [NSNumber numberWithBool:NO],
                                      [NSNumber numberWithBool:YES],
    
                                      ];
        NSArray *cellHeightArray = @[
                                      [NSNumber numberWithFloat:44],
                                      [NSNumber numberWithFloat:44],
                                      [NSNumber numberWithFloat:44],
                                      [NSNumber numberWithFloat:44],
                                      [NSNumber numberWithFloat:44],
                                      
                                      ];
        for (NSInteger index = 0; index < leftTitleArray.count; index++) {
            GHFromModel *fromModel = [[GHFromModel alloc]init];
            fromModel.leftTitle = leftTitleArray[index];
            NSNumber *isShowStartNum = isShowStartArray[index];
            NSNumber *cellHeightNum = cellHeightArray[index];
            NSNumber *cellTypeNum = cellTypeNumArray[index];
    
            fromModel.cellHeight = cellHeightNum.integerValue;
            fromModel.cellType = cellTypeNum.integerValue;
            fromModel.placeholder = placeholderArray [index];
            fromModel.imageName = imageNameArray[index];
            fromModel.isShowStar = isShowStartNum.integerValue;
            [dataArray addObject:fromModel];
        }
        return dataArray;
    }
    

    demo地址

    如果对你有帮助请给一个start

    相关文章

      网友评论

        本文标题:iOS 超简单1种cell高度自定义实现个人设置 &&a

        本文链接:https://www.haomeiwen.com/subject/mwlwaxtx.html