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
- iOS学习笔记 自定义cell并且文本自适应高度
- iOS 自定义Cell 自适应高度
- iOS编程------自定义UITableViewCell / cell自适应高度
- iOS Cell高度自适应
- ios cell自适应高度
- iOS cell 自适应高度
- iOS中cell自适应文本Label高度篇
- iOS cell自适应 自定义cell
- IOS文本自适应高度
- iOS 8 自适应 Cell高度
- IOS--UI Cell 自适应高度
- iOS实现Cell自适应高度
- iOS 之cell自适应高度
- cell根据文本标签内容自适应高度
- iOS cell高度自适应_动态调节cell高度
- Ios UILabel 自适应 文本高度
- iOS Label文本自适应高度
- UITableView中自定义Cell,自适应高度
- 一个极好的关于画图的网站
- ThinkPHP快速入门5_循环和控制输出,公共模板和模板布局
- oracle显示两个字段相除的结果
- Python:no encoding declared 错误
- 【工具】C# DataTable导出到Excel
- iOS学习笔记 自定义cell并且文本自适应高度
- 计算当前年月日属于该年第几个 月 双月 季度 半年 的算法
- svn 异常 failed–previous interrupted
- vc 与 js应用
- 用短信验证码SDK实现手机号注册功能
- 去掉文件的bom头
- 前端开发资源
- LeetCode Longest Substring Without Repeating Characters
- iOS 用CornerStone配置SVN,HTTP及svn简单使用说明