美文网首页
ios Tableviewcell添加TextView自适应高度

ios Tableviewcell添加TextView自适应高度

作者: 橘子味草莓 | 来源:发表于2018-03-29 17:40 被阅读185次

    Tableviewcell.h文件

    #import "WXXCHZS_EditOutRegimentInfo_Text_Cell.h"

    #import@interface WXXCHZS_EditOutRegimentInfo_Text_Cell : UITableViewCell

    @property (nonatomic,strong)UILabel *nameLa;

    @property (nonatomic,strong)TXL_placeholderTextView *contentLa;

    @end

    Tableviewcell.m文件

    #import "WXXCHZS_EditOutRegimentInfo_Text_Cell.h"

    @implementation WXXCHZS_EditOutRegimentInfo_Text_Cell

    -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

    {

        if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

            [self setupMain];

        }

        return self;

    }

    -(void)setupMain

    {

        self.nameLa=[[UILabel alloc]init];

        [self.nameLa setTextAlignment:NSTextAlignmentLeft];

        [self.nameLa setFont:Font13];

        [self.nameLa setTextColor:[UIColor blackColor]];

        [self.contentView addSubview:self.nameLa];

        [self.nameLa mas_makeConstraints:^(MASConstraintMaker *make){

            make.left.mas_equalTo(16);

            make.top.mas_equalTo(15);

            make.right.mas_equalTo(-15);

            make.height.mas_equalTo(13);

        }];

        self.contentLa=[[TXL_placeholderTextView alloc]init];

        [self.contentLa setTextAlignment:NSTextAlignmentLeft];

        [self.contentLa setFont:Font14];

        [self.contentLa setTextColor:[UIColor blackColor]];

        [self.contentView addSubview:self.contentLa];

        [self.contentLa mas_makeConstraints:^(MASConstraintMaker *make){

            make.left.mas_equalTo(16);

            make.top.mas_equalTo(43);

            make.right.mas_equalTo(-15);

            make.bottom.mas_equalTo(-10);

        }];

    }

    控制器ViewController

    #pragma mark -初始化

    -(UITableView *)tableView

    {

        if (!_tableView) {

            _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, KMainWidth, KMainHeight-kNavbarHeight) style:UITableViewStyleGrouped];

            _tableView.backgroundColor=LineColor;

            _tableView.delegate=self;

            _tableView.dataSource=self;

            _tableView.estimatedRowHeight=100;

            _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

            [_tableView registerClass:[WXXCHZS_EditOutRegimentInfo_Status_Cell class] forCellReuseIdentifier:@"WXXCHZS_EditOutRegimentInfo_Status_Cell"];

            [_tableView registerClass:[WXXCHZS_EditOutRegimentInfo_Text_Cell class] forCellReuseIdentifier:@"WXXCHZS_EditOutRegimentInfo_Text_Cell"];

            self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

                [self headerRefresh];

            }];

        }

        return _tableView;

    }

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        if (indexPath.section==0) {

            WXXCHZS_EditOutRegimentInfo_Status_Cell *cell=[tableView dequeueReusableCellWithIdentifier:@"WXXCHZS_EditOutRegimentInfo_Status_Cell"];

            if (self.dataDict.allKeys.count) {

                cell.selectionStyle=UITableViewCellSelectionStyleNone;

                [cell.headerImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.dataDict[@"authorFace"]]] placeholderImage:[UIImage imageNamed:@"bg_richeng-1"]];

                [cell.nameLa setText:[NSString stringWithFormat:@"%@",_dataDict[@"authorName"]]];

                [cell.phoneLa setText:[NSString stringWithFormat:@"%@",_dataDict[@"xchSn"]]];

                NSString * status;

                NSString *statusNss=_dataDict[@"tripStatus"];

                switch (statusNss.intValue) {

                    case 0:

                        status = @"未提交";

                        break;

                    case 1:

                        status = @"待确认";

                        break;

                    case 2:

                        status = @"待支付(订金)";

                        break;

                    case 3:

                        status = @"待支付(全款)";

                        break;

                    case 4:

                        status = @"已付款";

                        break;

                    case 5:

                        status = @"待出发";

                        break;

                    case 6:

                        status = @"进行中";

                        break;

                    case 7:

                        status = @"已完成";

                        break;

                    case 8:

                        status = @"已评价";

                        break;

                    case 9:

                        status = @"已关闭";

                        break;

                    default:

                        status = @"";

                        break;

                }

                [cell.stateLa setText:[NSString stringWithFormat:@"%@",status]];

            }

            return cell;

        }else{

            WXXCHZS_EditOutRegimentInfo_Text_Cell *cell=[tableView dequeueReusableCellWithIdentifier:@"WXXCHZS_EditOutRegimentInfo_Text_Cell"];

            cell.selectionStyle=UITableViewCellSelectionStyleNone;

            NSInteger row = [indexPath row];

            cell.contentLa.tag=row+100;

            cell.contentLa.scrollEnabled  =NO;

            cell.contentLa.textContainer.lineFragmentPadding = 0;

            cell.contentLa.textContainerInset = UIEdgeInsetsZero;

            cell.contentLa.autocorrectionType = UITextAutocorrectionTypeNo;

            cell.contentLa.autocapitalizationType = UITextAutocapitalizationTypeNone;

            cell.contentLa.returnKeyType = UIReturnKeyDefault;

            if (self.dataDict.allKeys.count) {

                if ([self.isMessage integerValue]==0) {

                    cell.contentLa.editable=NO;

                }else{

                    cell.contentLa.delegate=self;

                    cell.contentLa.editable=YES;

                    if (indexPath.row==0) {

                        [cell.contentLa setPlaceholder:@"请填写行程名称"];

                    }else if (indexPath.row==1){

                        [cell.contentLa setPlaceholder:@"请填写行程航班信息"];

                    }else if (indexPath.row==2){

                        [cell.contentLa setPlaceholder:@"请填写集合地点"];

                    }else if (indexPath.row==3){

                        [cell.contentLa setPlaceholder:@"请填写导游真实姓名"];

                    }else if (indexPath.row==4){

                        [cell.contentLa setPlaceholder:@"请填写紧急电话"];

                    }else{

                        [cell.contentLa setPlaceholder:@"请填写补充说明"];

                    }

                }

                if (indexPath.row==0){

                    [cell.nameLa setText:[NSString stringWithFormat:@"行程名称"]];

                    if ([self.dict[@"title"] length]>0) {

                        cell.contentLa.editable=NO;

                        [cell.contentLa setText:[NSString stringWithFormat:@"%@",self.dict[@"title"]]];

                    }

                }else if (indexPath.row==1){

                    [cell.nameLa setText:[NSString stringWithFormat:@"往返航班"]];

                    if ([self.dict[@"transportInfo"] length]>0) {

                        [cell.contentLa setText:[NSString stringWithFormat:@"%@",self.dict[@"transportInfo"]]];

                    }

                }else if (indexPath.row==2){

                    [cell.nameLa setText:[NSString stringWithFormat:@"集合安排"]];

                    if ([self.dict[@"jiheInfo"] length]>0) {

                        [cell.contentLa setText:[NSString stringWithFormat:@"%@",self.dict[@"jiheInfo"]]];

                    }

                }else if (indexPath.row==3){

                    [cell.nameLa setText:[NSString stringWithFormat:@"导游领队"]];

                    if ([self.dict[@"talentInfo"] length]>0) {

                        [cell.contentLa setText:[NSString stringWithFormat:@"%@",self.dict[@"talentInfo"]]];

                    }

                }else if (indexPath.row==4){

                    [cell.nameLa setText:[NSString stringWithFormat:@"紧急电话"]];

                    if ([self.dict[@"jinjiPhone"] length]>0) {

                        [cell.contentLa setText:[NSString stringWithFormat:@"%@",self.dict[@"jinjiPhone"]]];

                    }

                }else{

                    [cell.nameLa setText:[NSString stringWithFormat:@"补充说明"]];

                    if ([self.dict[@"remark"] length]>0) {

                        [cell.contentLa setText:[NSString stringWithFormat:@"%@",self.dict[@"remark"]]];

                    }

                }

            }

            return cell;

        }

    }

    - (void)textViewDidChange:(UITextView *)textView

    {

        if (textView.tag==100) {

            self.dict[@"title"] = textView.text;

        }else if (textView.tag==101){

            self.dict[@"transportInfo"] = textView.text;

        }else if (textView.tag==102){;

            self.dict[@"jiheInfo"] = textView.text;

        }else if (textView.tag==103){

            self.dict[@"talentInfo"] = textView.text;

        }else if (textView.tag==104){

            self.dict[@"jinjiPhone"] = textView.text;

        }else if (textView.tag==105){

            self.dict[@"remark"] = textView.text;

        }

        CGRect bounds = textView.bounds;

        //    计算 text view 的高度

        CGSize maxSize = CGSizeMake(KMainWidth-30, CGFLOAT_MAX);

        CGSize newSize = [textView sizeThatFits:maxSize];

        bounds.size=newSize;

        textView.bounds = bounds;

        // 让 table view 重新计算高度

        UITableView *tableView = [self tableView];

        [tableView beginUpdates];

        [tableView endUpdates];

    }

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    {

        return 2;

    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    {

        if (section==0) {

            return 1;

        }else{

            return 6;

        }

    }

    -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

    {

        if (section==0) {

            return 0.1;

        }else{

            return 10;

        }

    }

    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

    {

        if (section==0) {

            return nil;

        }else{

            UIView *headerView;

            if (!headerView) {

                headerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, KMainWidth, 10)];

                headerView.backgroundColor=LineColor;

            }

            return headerView;

        }

    }

    -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

    {

        if (section==1) {

            return 81;

        }else{

            return 0.1;

        }

    }

    -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

    {

        if (section==0) {

            return nil;

        }else{

            UIView *footerView;

            if (!footerView) {

                footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, KMainWidth, 81)];

                footerView.backgroundColor=LineColor;

                UIButton *saveBtn=[[UIButton alloc]initWithFrame:CGRectMake(15, 30, KMainWidth-30, 44)];

                [saveBtn setTitle:@"保存" forState:UIControlStateNormal];

                saveBtn.titleLabel.font=Font18;

                [saveBtn addTarget:self action:@selector(DoSaveClick) forControlEvents:UIControlEventTouchUpInside];

                [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

                saveBtn.backgroundColor=GreenMainColor;

                [footerView addSubview:saveBtn];

            }

            return footerView;

        }

    }

    - (void)setExtraCellLineHidden: (UITableView *)tableView

    {

        UIView *view =[ [UIView alloc]init];

        view.backgroundColor = [UIColor clearColor];

        [tableView setTableFooterView:view];

    }

    相关文章

      网友评论

          本文标题:ios Tableviewcell添加TextView自适应高度

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