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

#import "AlertsViewController.h"

#import "AlertsCell.h"

#import "AspectModel.h"

@interface AlertsViewController ()<UITableViewDataSource, UITableViewDelegate>

@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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、简介 <<UITableView(或简单地说,表视图)的一个实例是用于显示和编辑分层列出的信息的一种手段 <<...
    无邪8阅读 10,792评论 3 3
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,590评论 1 14
  • 哦吼吼,又研究了几天,把FMDB这个封装好的数据库搞定了,写了个简单的例子,基于FMDB的添删改查操作,界面很一般...
    lichengjin阅读 596评论 0 0
  • 作者唯一QQ:228544117。。。。。 =========后面的都要新建一个文章 AppDelegate.h ...
    CC_iOS阅读 1,022评论 0 0
  • 我们总是在害怕失去东西,因为觉得失去了就感觉很槽糕,代表自己得东西没了,然后就会后悔自己当初的决定,渴望再次得...
    假情贾意阅读 556评论 0 0