ios TableView那些事(三十 五)TableView 单选操作使用Autolayout实现UITableView的Cell动态布局和高度动态改变
2016-03-18 17:24
1051 查看
详解点击
使用Autolayout实现UITableView的Cell动态布局和高度动态改变
iOS8的示例代码
iOS7的示例代码
实战demo下载
实例代码
// ViewController.m
// SiziingDemo
//
// Created by lengshengren on 16/3/17.
// Copyright © 2016年 Rnning. All rights reserved.
//
@property (strong, nonatomic)NSArray *countArray;
@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 100.0;
self.countArray = @[@”今天真高兴”,@”传闻已久的2016苹果春季发布会时间终于尘埃落定,日前苹果在官网上发出邀请函,宣布将于美国东部时间3月21日下午1点(北京时间3月22日凌晨1点)举行发布会,地点设在苹果公司的加州库比蒂诺总部,邀请函的标题文案是“Let us loop you in”,@”【“阿尔法狗”真的让人担心吗?】围棋是人工智能领域的一个具有标志性的挑战,现在“阿尔法狗”战胜了世界冠军李世石,是否意味着霍金对人工智能的担忧将成为现实?人工智能对人类生存的威胁会超过核武器吗?对于这一重大问题,一些人工智能的支持者却不这么认为”,@”哈哈”];
// Do any additional setup after loading the view, typically from a nib.
}
(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(contentSizeCategoryChanged:)
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
(void)contentSizeCategoryChanged:(NSNotification *)notification
{
[self.tableView reloadData];
}
(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
return 1;
}
(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return 4;
}
(UITableViewCell )tableView:(UITableView )tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@”cell” forIndexPath:indexPath];
[self configureCell:cell forRowAtIndexPath:indexPath];
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
return cell;
}
(void)configureCell:(CustomTableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.countenLable.text = self.countArray[indexPath.row];
}
@end
使用Autolayout实现UITableView的Cell动态布局和高度动态改变
iOS8的示例代码
iOS7的示例代码
实战demo下载
实例代码
// ViewController.m
// SiziingDemo
//
// Created by lengshengren on 16/3/17.
// Copyright © 2016年 Rnning. All rights reserved.
//
import “ViewController.h”
import “CustomTableViewCell.h”
@interface ViewController ()@property (strong, nonatomic)NSArray *countArray;
@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 100.0;
self.countArray = @[@”今天真高兴”,@”传闻已久的2016苹果春季发布会时间终于尘埃落定,日前苹果在官网上发出邀请函,宣布将于美国东部时间3月21日下午1点(北京时间3月22日凌晨1点)举行发布会,地点设在苹果公司的加州库比蒂诺总部,邀请函的标题文案是“Let us loop you in”,@”【“阿尔法狗”真的让人担心吗?】围棋是人工智能领域的一个具有标志性的挑战,现在“阿尔法狗”战胜了世界冠军李世石,是否意味着霍金对人工智能的担忧将成为现实?人工智能对人类生存的威胁会超过核武器吗?对于这一重大问题,一些人工智能的支持者却不这么认为”,@”哈哈”];
// Do any additional setup after loading the view, typically from a nib.
}
(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(contentSizeCategoryChanged:)
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
(void)contentSizeCategoryChanged:(NSNotification *)notification
{
[self.tableView reloadData];
}
(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
pragma mark - UITableViewDataSource
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 1;
}
(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return 4;
}
(UITableViewCell )tableView:(UITableView )tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@”cell” forIndexPath:indexPath];
[self configureCell:cell forRowAtIndexPath:indexPath];
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
return cell;
}
(void)configureCell:(CustomTableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.countenLable.text = self.countArray[indexPath.row];
}
@end
相关文章推荐
- 菜鸟成长记-UICollectionView使用
- DMX512 for Arduino 例子详解
- easyui的treegrid开发使用
- 关于iOS`UITableView`中cell多选 全选自定义问题
- execSQL()和rawQuery()
- Subarray Substring vs Subsequence
- iOS--------坐标系统(UIView的frame、bounds跟center属性)
- Adobe Flash Builder 4.7破解方法(绝对可用)
- 微交互怎么玩?一份简单实用的微交互设计指南(超多案例)
- 实验StringBuilder类实现字符反转
- GUI登录界面
- 输入框的value问题(DOM的property和attribute)
- iOS : 通过Nib/Xib来加载UIView的注意事项
- 关于UITableView段落模式 — 段头上的button问题
- ElasticSearch的 Query DSL 和 Filter DSL
- 基础控件属性及方法整理 --> UIButton
- 搜狐设计师:我从产品经理转为交互设计师的经验总结
- AndroidStudio引入环信easeui库出错
- MSBuild和Jenkins搭建持续集成环境(2)
- 1加1在什么情况下等于3