ios Tableviewcell添加TextView自適應(yīng)高度

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 = @"待確認(rèn)";

? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? case 2:

? ? ? ? ? ? ? ? ? ? status = @"待支付(訂金)";

? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? case 3:

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

? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? case 4:

? ? ? ? ? ? ? ? ? ? status = @"已付款";

? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? case 5:

? ? ? ? ? ? ? ? ? ? status = @"待出發(fā)";

? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? case 6:

? ? ? ? ? ? ? ? ? ? status = @"進(jìn)行中";

? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? case 7:

? ? ? ? ? ? ? ? ? ? status = @"已完成";

? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? case 8:

? ? ? ? ? ? ? ? ? ? status = @"已評(píng)價(jià)";

? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? case 9:

? ? ? ? ? ? ? ? ? ? status = @"已關(guān)閉";

? ? ? ? ? ? ? ? ? ? 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:@"請(qǐng)?zhí)顚懶谐堂Q"];

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

? ? ? ? ? ? ? ? ? ? [cell.contentLa setPlaceholder:@"請(qǐng)?zhí)顚懶谐毯桨嘈畔?];

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

? ? ? ? ? ? ? ? ? ? [cell.contentLa setPlaceholder:@"請(qǐng)?zhí)顚懠系攸c(diǎn)"];

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

? ? ? ? ? ? ? ? ? ? [cell.contentLa setPlaceholder:@"請(qǐng)?zhí)顚憣?dǎo)游真實(shí)姓名"];

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

? ? ? ? ? ? ? ? ? ? [cell.contentLa setPlaceholder:@"請(qǐng)?zhí)顚懢o急電話"];

? ? ? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? ? ? [cell.contentLa setPlaceholder:@"請(qǐng)?zhí)顚懷a(bǔ)充說明"];

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? 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:@"導(dǎo)游領(lǐng)隊(duì)"]];

? ? ? ? ? ? ? ? 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:@"補(bǔ)充說明"]];

? ? ? ? ? ? ? ? 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;

? ? //? ? 計(jì)算 text view 的高度

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

? ? CGSize newSize = [textView sizeThatFits:maxSize];

? ? bounds.size=newSize;

? ? textView.bounds = bounds;

? ? // 讓 table view 重新計(jì)算高度

? ? 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];

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,282評(píng)論 3 38
  • 1.badgeVaule氣泡提示 2.git終端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夾內(nèi)容...
    i得深刻方得S閱讀 4,968評(píng)論 1 9
  • 1、禁止手機(jī)睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa閱讀 1,199評(píng)論 1 6
  • 20171220打卡 目標(biāo):健康、夫妻和諧、自我成長 好種子: 1.經(jīng)絡(luò)操15分,健康的種子(...
    南哲玲閱讀 157評(píng)論 0 0
  • 洛陽才子他鄉(xiāng)逢,身家渴求萬谷鐘。 豐彩幾欲迷人眼,背后辛酸應(yīng)相同。
    陽春閱讀 209評(píng)論 0 5

友情鏈接更多精彩內(nèi)容