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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值