美文网首页
动态修改UITableView footerView的高度

动态修改UITableView footerView的高度

作者: 东了个尼 | 来源:发表于2019-04-01 17:12 被阅读0次

    在开发中遇到了一个需求就是动态修改footerView的高度,footerView的高度随着用户输入内容的变化,改变输入框的高度,并且改变footerView的高度,通常情况下我们会直接创建footerView,并且给footer一个frame,但是如果想动态修改footerView的高度,或者让footerView高度自适应,我们就需要需改footerView的高度,并且重新赋值给tableView

    CGRect newFrame = viewHeader.frame;
    newFrame.size.height = newFrame.size.height + webView.frame.size.height;
    viewHeader.frame = newFrame;
    [self.tableView setTableHeaderView: viewHeader];
    
    @interface ETCAuditViewController ()<UITableViewDelegate,UITableViewDataSource>
    @property(nonatomic,strong)UITableView *mineTableView;
    @property(nonatomic,strong)AuditFooterView *footerView;
    @end
    
    @implementation ETCAuditViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.navigationItem.title = AppLocalString(@"bulk_task_list_query");
        self.edgesForExtendedLayout = UIRectEdgeNone;
        [self.view addSubview:self.mineTableView];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeFooterViewHeight:) name:@"changeHeight" object:nil];
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        AuditTableViewCell *cell = [AuditTableViewCell cellWithTableView:tableView];
        return cell;
    }
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
            
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return 10;
    }
    - (UITableView *)mineTableView{
        if (!_mineTableView) {
            if (kDevice_iPhoneX) {
                _mineTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-TotaliPhoneX) style:UITableViewStylePlain];
                NSLog(@"SCREEN_HEIGHT=%f  TotaliPhoneX = %f",SCREEN_HEIGHT,TotaliPhoneX);
            }else{
                _mineTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT-NAVIGATION_BAR_HEIGHT) style:UITableViewStylePlain];
            }
            _mineTableView.backgroundColor = TableViewBackColor;
            _mineTableView.delegate = self;
            _mineTableView.dataSource = self;
            _mineTableView.rowHeight = UITableViewAutomaticDimension;
            _mineTableView.separatorColor = [UIColor whiteColor];
            _mineTableView.estimatedRowHeight = 160;
            AuditHeaderView *headerView = [[AuditHeaderView alloc] init];
            headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 30);
            _mineTableView.tableHeaderView = headerView;
            self.footerView = [[AuditFooterView alloc] init];
            _footerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 120);
            _mineTableView.tableFooterView = _footerView;
        }   
        return _mineTableView;
    }
    - (void)changeFooterViewHeight:(NSNotification *)note{
        NSDictionary *dic = note.userInfo;
        CGFloat totalHeight =  [dic[@"totalHeight"] floatValue];
        self.footerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, totalHeight);
        [self.mineTableView.tableFooterView setNeedsLayout];
        self.mineTableView.tableFooterView = self.footerView;
    }
    
    

    自定义footerView内部

    #import "AuditFooterView.h"
    @interface AuditFooterView()<UITextViewDelegate>
    {
        CGFloat _initHeight;
    
    }
    @end
    @implementation AuditFooterView
    - (instancetype)init{
        self = [super init];
        if (self) {
            self = [[[NSBundle mainBundle] loadNibNamed:@"AuditFooterView" owner:self options:nil] lastObject];
            self.opinion.delegate = self;
            _initHeight  = 30;
            self.opinion.layer.borderColor = [UIColor greenColor].CGColor;
    
            self.opinion.layer.borderWidth = 1.0f;
            self.opinion.textContainerInset =  UIEdgeInsetsMake(2, 0, 0, 0);
        }
        return self;
    }        
    - (void)awakeFromNib{
        [super awakeFromNib];
    }
    /** textView文本内容改变时回调 */
    - (void)textViewDidChange:(UITextView *)textView{
        // 计算高度
        CGSize size = CGSizeMake(self.opinion.frame.size.width, CGFLOAT_MAX);
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:self.opinion.font,NSFontAttributeName, nil];
        CGFloat curheight = [textView.text boundingRectWithSize:size
                                                        options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                                     attributes:dic
                                                        context:nil].size.height;
        // 如果高度小于初始化时的高度,则不赋值(仍采用最初的高度)
        if (curheight < _initHeight) {
    //        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, _initHeight);
    //        self.opinion.frame = CGRectMake(self.opinion.frame.origin.x, self.opinion.frame.origin.y, self.opinion.frame.size.width, _initHeight);
            self.textViewHeight.constant = _initHeight;
            CGFloat totalHeight = 120;
            [[NSNotificationCenter defaultCenter] postNotificationName:@"changeHeight" object:nil userInfo:@{@"totalHeight":[NSString stringWithFormat:@"%f",totalHeight]}];
        }else{
            // 重新给frame赋值(改变高度)
    //        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, curheight+20);
    //        self.opinion.frame = CGRectMake(self.opinion.frame.origin.x, self.opinion.frame.origin.y, self.opinion.frame.size.width, curheight+20);
            self.textViewHeight.constant = curheight+20;
            CGFloat totalHeight = 120 + curheight  - 30;
            [[NSNotificationCenter defaultCenter] postNotificationName:@"changeHeight" object:nil userInfo:@{@"totalHeight":[NSString stringWithFormat:@"%f",totalHeight]}];
        }
    
    //    // 如果文本为空,显示placeholder
    //    if (textView.text.length == 0) {
    //        self.placeholderLabel.hidden = NO;
    //        self.placeholderLabel.center = self.textView.center;
    //    }else{
    //        self.placeholderLabel.hidden = YES;
    //    }
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:动态修改UITableView footerView的高度

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