ios TableView那些事(三十 五)TableView 单选操作使用Autolayout实现UITableView的Cell动态布局和高度动态改变
2016-03-17 11:10
846 查看
详解点击 使用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
“`
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
“`
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用中UISearchDisplayController搜索效果的用法
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- iOS推送的那些事
- IOS 改变键盘颜色代码