iOS UITableViewCell自適應高度的例子

來源:互聯網
上載者:User

例如淘寶購買完商品後的評價,評價過的評價列表裡,每個人評價的內容不同,評價內容有多有少,我們一般都是用UITableView來建立介面的,這時候就需要cell自適應高度了。程式碼範例:

 代碼如下 複製代碼

EvaluateTableViewCell.h

#import <UIKit/UIKit.h>
 
@interface EvaluateTableViewCell : UITableViewCell
@property (nonatomic,strong) UILabel *phoneLabel;
@property (nonatomic,strong) UILabel *timeLabel;
@property (nonatomic,strong) UILabel *descLabel;
@property (nonatomic,strong) UIView *intervalView;
 
//評價內容並且實現自動換行
-(void)setIntroductionText:(NSString*)text;
 
@end


EvaluateTableViewCell.m

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //使用者手機號
        self.phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(7, 15, 100, 12)];
        self.phoneLabel.font = FONT(12);
        [self addSubview:self.phoneLabel];
        //評價時間
        self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(247 * (kScreenWidth / 320), 15, 72, 11)];
        self.timeLabel.font = FONT(11);
        self.timeLabel.textColor = [UIColor grayColor];
        [self addSubview:self.timeLabel];
        //分隔線
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(8, self.timeLabel.maxY + 14, kScreenWidth - 16, 1)];
        lineView.backgroundColor = [UIColor blackColor];
        [self addSubview:lineView];
        //評價內容
        self.descLabel = [[UILabel alloc] initWithFrame:CGRectMake(11, lineView.maxY+ 29, kScreenWidth - 22, 13)];
        self.descLabel.font = FONT(12);
        self.descLabel.numberOfLines = 0;
        [self addSubview:self.descLabel];
        //間隔
        self.intervalView = [[UIView alloc] initWithFrame:CGRectMake(0, self.descLabel.maxY + 9, kScreenWidth, 2)];
        self.intervalView.backgroundColor = [UIColor blackColor];
        [self addSubview:self.intervalView];
    }
    return self;
}
//賦值 and 自動換行,計算出cell的高度
-(void)setIntroductionText:(NSString*)text{
    //獲得當前cell高度
    CGRect frame = [self frame];
    //文本賦值
    self.descLabel.text = text;
    //設定label的最大行數
    CGSize size = CGSizeMake(300, 1000);
    CGSize labelSize = [self.descLabel.text sizeWithFont:self.descLabel.font constrainedToSize:size lineBreakMode:NSLineBreakByClipping];
    self.descLabel.frame = CGRectMake(self.descLabel.frame.origin.x, self.descLabel.frame.origin.y, labelSize.width, labelSize.height);
    
    //計算出自適應的高度
    frame.size.height = labelSize.height + 85;
    self.frame = frame;
    self.intervalView.frame = CGRectMake(0, self.descLabel.maxY + 9, kScreenWidth, 4);
}


 

cellzishiyingViewController.m

 代碼如下 複製代碼


#import "cellzishiyingViewController.h"
#import "EvaluateTableViewCell.h"
 
@interface cellzishiyingViewController ()<UITableViewDataSource,UITableViewDelegate>{
    UITableView *evaluateTableView;
    UILabel *evaluateLabel;
    UILabel *goodEvaluateLabel;
}
 
@end
 
@implementation cellzishiyingViewController
 
 
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGB(242, 242, 247);
    self.automaticallyAdjustsScrollViewInsets = NO;
    evaluateTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kHeaderHeight, kScreenWidth, kScreenHeight - kHeaderHeight) style:UITableViewStylePlain];
    evaluateTableView.delegate = self;
    evaluateTableView.dataSource = self;
    evaluateTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:evaluateTableView];
}
#pragma mark - 行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}
#pragma mark - 行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //    return 95;
    EvaluateTableViewCell *cell = [self tableView:evaluateTableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}
#pragma mark - 每行內容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indefier = @"cell";
    EvaluateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indefier];
    if (!cell) {
        cell = [[EvaluateTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indefier];
    }
    NSArray *oneArray = @[@"18374637823",@"18643535325",@"15653543746",@"13924366238"];
    NSArray *twoArray = @[@"2016-04-04",@"2016-04-06",@"2016-05-04",@"2016-09-04"];
    NSArray *threeArray = @[@"由於iOS是遵循MVC模式設計的,很多操作都是通過代理和外界溝通的,但對於資料來源控制項除了代理還有一個資料來源屬性,通過它和外界進行資料互動。",@"UITableView有兩種風格:UITableViewStylePlain和UITableViewStyleGrouped。這兩者操作起來其實並沒有本質區別,只是後者按分組樣式顯示前者按照普通樣式顯示而已",@"由於iOS是遵循MVC模式設計的,很多操作都是通過代理和外界溝通的,但對於資料來源控制項除了代理還有一個資料來源屬性,通過它和外界進行資料互動。 對於UITableView設定完dataSource後需要實現UITableViewDataSource協議,在這個協議中定義了多種 資料操作方法",@"切實開展批評和自我批評,勇於揭露和糾正工作中"];
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.phoneLabel.text = oneArray[indexPath.row];
    cell.timeLabel.text =  twoArray[indexPath.row];
    cell.descLabel.text = threeArray[indexPath.row];
    [cell setIntroductionText:cell.descLabel.text];
    return cell;
}

註:

 代碼如下 複製代碼


// 當前螢幕寬度
#define kScreenWidth    [UIScreen mainScreen].bounds.size.width
// 當前螢幕高度
#define kScreenHeight   [UIScreen mainScreen].bounds.size.height

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.