iOS学习笔记 自定义cell并且文本自适应高度
2015-11-10 09:29
603 查看
UITableViewCell自定义:
其中要注意的地方:计算cell高度 给lable再次赋文本 单独计算高度;
cusTableViewCell.h文件
(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
}
return self;
}
(void)setlabel2Text:(NSString *)text2
{
_label2.text = text2;
}
@end
cusTableViewController.m文件
{
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”];
}
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) {
//图片显示在左边
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{
}
@end
其中要注意的地方:计算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
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- IOS 改变键盘颜色代码
- 举例详解iOS开发过程中的沙盒机制与文件
- Android和IOS的浏览器中检测是否安装某个客户端的方法
- 分享一个iOS下实现基本绘画板功能的简单方法
- javascript实现阻止iOS APP中的链接打开Safari浏览器
- IOS开发第三方语音-微信语音