详解点击 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
实战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
“`