您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 使用技巧 >> iOS 7 技巧

iOS 開發:讓 UITableViewCell 高度自適應的完整示例

編輯:iOS 7 技巧

關於 iOS 中的 UITableViewCell,此前已介紹過不少內容,今天再來看一個讓 cell 高度自適應的實現方案,具體細節如下。

舉個常見例子:在淘寶完成購物後撰寫評價,評價列表裡每個人的內容長短不一,有多有少。我們通常用 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

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved