UI_11 自定義UITableViewCell、Cell的高度自適應

     UITableViewCell很難知足咱們的需求,所以,CustomCell(自定義單元格)相當重要。下面將經過一個例子演示自定義Cell。第二部分演示根據文本內容自適應Label、Cell高度。app



第一部分 CustomCell的建立測試

一、建立DemoTableViewController,繼承自UITableViewController,並設置其爲window的根視圖atom

AppDelegate.mspa

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];orm

    DemoTableViewController *demoVC = [[DemoTableViewController alloc] initWithStyle:UITableViewStylePlain];繼承

    self.window.rootViewController = demoVC;字符串

    self.window.backgroundColor = [UIColor whiteColor];get

    [self.window makeKeyAndVisible];
   
return YES;it

}io


二、建立CustomTableViewCell,繼承自UITableViewCell

聲明CustomCell中所需屬性:

@property(nonatomic, retain) UILabel *customLabel;

重寫初始化方法:

CustomTableViewCell.m

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
   
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
   

    if (self) {

        self.customLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 100, 30)];

        _customLabel.backgroundColor = [UIColor blueColor];
        [
self.contentView addSubview:_customLabel];

    }

    return self;

}


DemoTableViewController.m

三、設置TableView的section和row均爲1

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

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

    return 1;

}


四、加載單元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (!cell) {

        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    cell.customLabel.text = @"Hello";

    return cell;

}



第二部分 Cell高度自適應

     使用NSString的類目 @interface NSString (NSExtendedStringDrawing)

中的方法

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context NS_AVAILABLE_IOS(7_0);


一、建立getTextHeight:方法。參數設爲UILabel

- (CGFloat)getTextHeight:(UILabel *)label

{

    /*

        Size:估算高度。

若是計算結果大於Size,則取小於Size的最大值做爲返回值,不然,直接返回結果

        options:枚舉值,表示以何種方式計算

     NSStringDrawingUsesLineFragmentOrigin 以行高爲單位累加;還表示Size(第一個參數)的高度將被忽略

     NSStringDrawingUsesFontLeading 行高(包括行間距)

        attributes: @{NSFontAttributeName:font}

        context:NULL

     */

    CGSize maxSize = CGSizeMake(label.frame.size.width, 100);   //最大行高,即下面方法計算出來的行高小於maxSize.height

    CGRect rect = [label.text
                  
boundingRectWithSize:maxSize
                  
options:NSStringDrawingUsesLineFragmentOrigin
                  
attributes:@{NSFontAttributeName:label.font}
                  
context:NULL];
   
return rect.size.height;

}

二、聲明textString屬性做爲測試字符串,並在viewDidLoad中爲其賦值

DemoTableView.m

@interface DemoTableViewController ()
@property (nonatomic, retain) NSString *textString;
@end

@implementation DemoTableViewController

- (void)viewDidLoad {
    [
super viewDidLoad];

    self.textString = [[UILabel alloc] init];
   
_textString.text = @"第一行。\n第二行。。\n第三行。。。\n第四行。。。。\n第五行\n第六行";

}


三、在下面方法中增長內容設置customLabel高度

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
   
   
if (!cell) {
        cell = [[
CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
   
   
//設置customLabel高度

    cell.customLabel.text = _textString.text;
    cell.
customLabel.numberOfLines = 0;
   
   
CGRect rect = cell.customLabel.frame;
    rect.
size.height = [self getTextHeight:cell.customLabel];
    cell.
customLabel.frame = rect;

    

    return cell;

}


四、計算並設置cell高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
return [self getTextHeight:_textString]+10;

}

相關文章
相關標籤/搜索