iOS学习笔记 自定义cell并且文本自适应高度

来源:互联网 发布:拜占庭共识算法 编辑:程序博客网 时间:2024/05/16 09:43

UITableViewCell自定义:
其中要注意的地方:计算cell高度 给lable再次赋文本 单独计算高度;


cusTableViewCell.h文件

import

import “cusTableViewCell.h”

@implementation cusTableViewCell

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

    if (self) {

    //详细内容文本_label2 = [[UILabel alloc] init];_label2.translatesAutoresizingMaskIntoConstraints = NO;_label2.font = [UIFont systemFontOfSize:14.0f];_label2.numberOfLines = 0; //相当于不限制行数_label2.lineBreakMode = NSLineBreakByWordWrapping;[self.contentView addSubview:_label2];_label2.preferredMaxLayoutWidth = self.contentView.frame.size.width;//VFL设置图片等约束NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_label2);[self.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_label2]-|"                                         options:0                                         metrics:nil                                           views:viewsDictionary]];//垂直方向[self.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[_label2]-|"                                         options:0                                         metrics:nil                                           views:viewsDictionary]];

    }
    return self;
    }

  • (void)setlabel2Text:(NSString *)text2
    {

    _label2.text = text2;
    }

@end


cusTableViewController.m文件

import “cusTableViewController.h”

import “cusTableViewCell.h”

import “ButtonViewController.h”

@interface cusTableViewController ()
{
cusTableViewCell * HeightCell;
}

@property (nonatomic, strong) cusTableViewCell *cell;
@end

@implementation cusTableViewController

  • (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @”自定义Cell”;
    self.view.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:(235/255.0) alpha:1.0f];

    //导航栏的颜色
    self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];

    //文本自适应 一行代码搞定
    // self.tableView.estimatedRowHeight = 200; //值随便给
    // self.tableView.rowHeight = UITableViewAutomaticDimension;

    HeightCell=[[cusTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@”idcell”];
    }

pragma mark - Table view data source

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
    }

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
    }

  • (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @”idcell”;

    _cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (_cell == nil) {
    _cell = [[cusTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    if (indexPath.section==0) {
    //图片显示在左边

    [_cell setlabel2Text:@"【实体全键盘+双曲面侧屏:黑莓Priv安卓机上手体验】黑莓首款安卓手机Priv今天算是正式发布了,但他们貌似省去了这一过程,官网已经接受预定,售价699美元,约合人民币4440元。" ]; } else {  //图片显示在右边[_cell setlabel2Text:@"【iOS 蓝牙开发(四)BabyBluetooth蓝牙库介绍】最近几个月都在做蓝牙项目,用CoreBluetooch感觉语句写的到处都是,不优雅。因此我就想让coreBlueTooth使用更简单,语法更优雅,所以开始写这个BabyBluetooch蓝牙库。【iOS 蓝牙开发(四)BabyBluetooth蓝牙库介绍】最近几个月都在做蓝牙项目,用CoreBluetooch感觉语句写的到处都是,不优雅。因此我就想让coreBlueTooth使用更简单,语法更优雅,所以开始写这个BabyBluetooch蓝牙库。"];   }

    return _cell;

}

  • (nullable NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section{

    if(section ==0){
    NSString *headerText = @”样式一”;
    return headerText;
    }

    NSString *headerText2 = @”样式二”;
    return headerText2;
    }

  • (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath{
    ButtonViewController *buttonVC = [[ButtonViewController alloc]init];
    [self.navigationController pushViewController:buttonVC animated:YES];
    }

  • (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath{

    //HeightCell
    [self SetCell:HeightCell path:indexPath];

    //自适应高度
    [HeightCell updateConstraints];
    [HeightCell layoutIfNeeded];

    CGFloat height = [HeightCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

    NSLog(@”%f”,height);

    return height+0.1f;

}

-(void)SetCell:(cusTableViewCell*)hCell path:(NSIndexPath*)indexPath{

if (indexPath.section==0){hCell.label2.text=@"【实体全键盘+双曲面侧屏:黑莓Priv安卓机上手体验】黑莓首款安卓手机Priv今天算是正式发布了,但他们貌似省去了这一过程,官网已经接受预定,售价699美元,约合人民币4440元。";}else{hCell.label2.text=@"【iOS 蓝牙开发(四)BabyBluetooth蓝牙库介绍】最近几个月都在做蓝牙项目,用CoreBluetooch感觉语句写的到处都是,不优雅。因此我就想让coreBlueTooth使用更简单,语法更优雅,所以开始写这个BabyBluetooch蓝牙库。【iOS 蓝牙开发(四)BabyBluetooth蓝牙库介绍】最近几个月都在做蓝牙项目,用CoreBluetooch感觉语句写的到处都是,不优雅。因此我就想让coreBlueTooth使用更简单,语法更优雅,所以开始写这个BabyBluetooch蓝牙库。";}

}
@end

0 0
原创粉丝点击