根据内容动态设置tableview中的label的高度

#import "AlertsViewController.h"

#import "AlertsCell.h"

#import "AspectModel.h"

@interface AlertsViewController ()

@property (strong, nonatomic)NSMutableArray *dataArr;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (assign, nonatomic)NSInteger pageIndex;

@end

@implementationAlertsViewController

-(NSMutableArray *)dataArr

{

    if(_dataArr==nil) {

        _dataArr= [[NSMutableArrayalloc]init];

    }

    return _dataArr;

}

- (void)viewDidLoad {

    [super viewDidLoad];

    self.title=@"快讯";

    self.tableView.frame=CGRectMake(0, 0, kScreenWidth, kScreenHeight-NaviH+TabidifferH);

    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;

    [self.tableView registerNib:[UINib nibWithNibName:@"AlertsCell" bundle:nil] forCellReuseIdentifier:@"XibCell"];

    //    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;


    self.pageIndex=1;

    [self loadData];


    //上拉加载

    self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{

        self.pageIndex++;

        [selfloadData];

    }];


}

-(void)loadData{

    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@(self.pageIndex),@"page", nil];

    [[WJLCConnect shareInstance]requestWithWJLCUrlType:CngoldkxChannelUrl params:params result:^(NSDictionary *obj) {


        if(self.pageIndex==1) {

            self.dataArr=[AspectModel objectArrayWithKeyValuesArray:obj[@"data"]];


        }else{

            [self.dataArr addObjectsFromArray:[AspectModel objectArrayWithKeyValuesArray:obj[@"data"]]];


        }

//        self.dataArr=[AspectModel objectArrayWithKeyValuesArray:obj[@"data"]];


        [self.tableViewreloadData];

        [MBProgressHUD hideHUDForView:self.view animated:YES];

        [self.tableView.mj_footer endRefreshing];


    }fail:^{

        [MBProgressHUD hideHUDForView:self.view animated:YES];

        [self.tableView.mj_footer endRefreshing];


    }];


}

- (void)viewWillLayoutSubviews {

    [super viewWillLayoutSubviews];

}

#pragma  mark-----TableView代理

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

    return self.dataArr.count;

}

-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{


    AspectModel  *model=self.dataArr[indexPath.row];


    return20+[model.contentHeightfloatValue]+25;

}

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

    AlertsCell *cell=[tableView dequeueReusableCellWithIdentifier:@"XibCell"];

    AspectModel*model=self.dataArr[indexPath.row];

    [cellrefreshDataModel:model];


    cell.selectionStyle = UITableViewCellSelectionStyleNone;


    returncell;


}

@end


#import "AspectModel.h"

NS_ASSUME_NONNULL_BEGIN

@interface AlertsCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *timeLab;

@property (weak, nonatomic) IBOutlet UILabel *titleLab;

-(void)refreshDataModel:(AspectModel*)model;

@end

NS_ASSUME_NONNULL_END


#import "AlertsCell.h"

@implementation AlertsCell

- (void)awakeFromNib {

    [super awakeFromNib];

    // Initialization code

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [supersetSelected:selectedanimated:animated];

    // Configure the view for the selected state

}

-(void)refreshDataModel:(AspectModel*)model{

    self.titleLab.text=[NSStringstringWithFormat:@"%@",model.title];

    self.timeLab.text=[NSStringstringWithFormat:@"%@",model.time];


    model.contentHeight = [NSString stringWithFormat:@"%f",[self.titleLab.text boundingRectWithSize:CGSizeMake(kScreenWidth-32, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size.height];

    self.titleLab.frame=CGRectMake(23,15,kScreenWidth-32, [model.contentHeightfloatValue]+25);


}

@end

你可能感兴趣的:(根据内容动态设置tableview中的label的高度)