zoukankan      html  css  js  c++  java
  • AutoLayout UITableViewCell 动态高度

     

    从这里http://www.cnblogs.com/liandwufan/p/4516956.html?utm_source=tuicool 转载过来的

     

    -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        //

        static NSString * cellid = @"PayMentTableViewCell";

        

        PayMentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellid];

        

        if( !cell )

        {

            cell = [[[NSBundle mainBundle] loadNibNamed:cellid owner:self options:nil] lastObject];

     

        }

        

        [cell refresh:[eventArray objectAtIndex:indexPath.row]];

        

        

        [cell setNeedsUpdateConstraints];

        [cell updateConstraintsIfNeeded];

        

        return cell;

    }

     

    #pragma mark -

    #pragma mark table view delegate methods

     

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        // 判断indexPath对应cell的重用标示符,

        static NSString *reuseIdentifier =@"PayMentTableViewCell";

        

        // cell字典中取出重用标示符对应的cell。如果没有,就创建一个新的然后存储在字典里面。

        // 警告:不要调用table viewdequeueReusableCellWithIdentifier:方法,因为这会导致cell被创建了但是又未曾被tableView:cellForRowAtIndexPath:方法返回,会造成内存泄露!

        PayMentTableViewCell *cell = [offscreenCells objectForKey:reuseIdentifier];

        

        if (!cell)

        {

            cell = [[[NSBundle mainBundle] loadNibNamed:reuseIdentifier owner:self options:nil] lastObject];

            

            [offscreenCells setObject:cell forKey:reuseIdentifier];

            

            [cell refresh:[eventArray objectAtIndex:indexPath.row]];

        }

        

        

        

        // indexPath对应的数据内容来配置cell,例如:

        // cell.textLabel.text = someTextForThisCell;

        // ...

        

        // 确保cell的布局约束被设置好了,因为它可能刚刚才被创建好。

        // 使用下面两行代码,前提是假设你已经在cellupdateConstraints方法中设置好了约束:

        [cell setNeedsUpdateConstraints];

        [cell updateConstraintsIfNeeded];

        

        // cell的宽度设置为和tableView的宽度一样宽。

        // 这点很重要。

        // 如果cell的高度取决于table view的宽度(例如,多行的UILabel通过单词换行等方式),

        // 那么这使得对于不同宽度的table view,我们都可以基于其宽度而得到cell的正确高度。

        // 但是,我们不需要在-[tableView:cellForRowAtIndexPath]方法中做相同的处理,

        // 因为,cell被用到table view中时,这是自动完成的。

        // 也要注意,一些情况下,cell的最终宽度可能不等于table view的宽度。

        // 例如当table view的右边显示了section index的时候,必须要减去这个宽度。

        cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds));

        

        // 触发cell的布局过程,会基于布局约束计算所有视图的frame

        // (注意,你必须要在cell-[layoutSubviews]方法中给多行的UILabel设置好preferredMaxLayoutWidth值;

        // 或者在下面2行代码前手动设置!)

        [cell setNeedsLayout];

        [cell layoutIfNeeded];

        

        // 得到cellcontentView需要的真实高度

        CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

        

        // 要为cell的分割线加上额外的1pt高度。因为分隔线是被加在cell底边和contentView底边之间的。

        height += 1.0f;

        

        return height;

    }

     

    // 注意:除非行高极端变化并且你已经明显的觉察到了滚动时滚动条的跳跃现象,你才需要实现此方法;否则,直接用tableViewestimatedRowHeight属性即可。

    - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        // 以必需的最小计算量,返回一个实际高度数量级之内的估算行高。

        // 例如:

        return 200;

    }

     

    //在自定义UITableViewCell 类中 添加函数

    - (void)layoutSubviews

    {

        [super layoutSubviews];

        _remarkLab.preferredMaxLayoutWidth = self.contentView.frame.size.width - (10+40+2+34+(25+15));

        _placeLab.preferredMaxLayoutWidth = _remarkLab.preferredMaxLayoutWidth;

        [super layoutSubviews];

    }

     

    preferredMaxLayoutWidth的值网上的方法都是不对的,我这里用了自己的方法来实现。

    以上是第一种方法,我的系统版本是 8.1   上面的是可以正常显示的,低于8的系统版本没有尝试

     ////////// ////////// ////////// ////////// ////////// ////////// ////////// ////////// ////////// /////////

     

    这种方法同样在ios8系统里面是OK的,在heightForRowAtIndexPath  里面有一个8以及8以下的区分 需要注意,

     

    #import "MainViewController.h"

    #import "TableViewCellA.h"

    #import "QuoteTableViewCell.h"

     

     

    #define SYSTEM_VERSION                              ([[UIDevice currentDevice] systemVersion])

    #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([SYSTEM_VERSION compare:v options:NSNumericSearch] != NSOrderedAscending)

    #define IS_IOS8_OR_ABOVE                            (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))

     

     

     

    @interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>

    {

        TableViewCellA * reduseCell;

    }

    @property (weak, nonatomic) IBOutlet UITableView *tableView;

     

    @end

     

    @implementation MainViewController

     

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Do any additional setup after loading the view from its nib.

    }

     

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

     

     

     

    #pragma UITableView

     

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    {

        return 10;

    }

     

    -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        TableViewCellA * cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCellA"];

        

        if( !cell )

        {

            cell = [[[NSBundle mainBundle] loadNibNamed:@"TableViewCellA" owner:self options:nil]lastObject];

        }

        

        [cell refreshCell:@"-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath-(UITableViewCell*)tableView:(UITableView *)tableView cellForRow"];

        

        return cell;

    }

     

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        //if( IS_IOS8_OR_ABOVE )

        {

            return UITableViewAutomaticDimension;

        }

        

        /*

        if (!reduseCell)

        {

            reduseCell = [self.tableView dequeueReusableCellWithIdentifier:@"TableViewCellA"];

        }

        

        

        [reduseCell refreshCell:@"-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath-(UITableViewCell*)tableView:(UITableView *)tableView cellForRow"];

     

     

        

        [reduseCell updateConstraintsIfNeeded];

        [reduseCell layoutIfNeeded];

        

        return [reduseCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

         */

    }

     

     

     

     

    -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        return UITableViewAutomaticDimension;

    }

     

     

     

     

     

    @end

     

     

     

    #import "TableViewCellA.h"

     

    @interface TableViewCellA()

    @property (weak, nonatomic) IBOutlet UILabel *textLab;

     

    @end

     

    @implementation TableViewCellA

     

    - (void)awakeFromNib {

        // Initialization code

    }

     

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {

        [super setSelected:selected animated:animated];

     

        // Configure the view for the selected state

    }

     

     

    -(void)layoutSubviews

    {

        [super layoutSubviews];

        

        [self.contentView updateConstraintsIfNeeded];

        [self.contentView layoutIfNeeded];

        

        self.textLab.preferredMaxLayoutWidth = self.textLab.frame.size.width;    

    }

     

    -(void)refreshCell:(NSString*)str

    {

        self.textLab.text = str;

    }

    @end

    下面是另外一种方法,

     

  • 相关阅读:
    mysql配置utf8_mb4
    Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by serv
    MongoDB自动删除过期数据--TTL索引
    正则小公举
    苹果手机弹起输入框将页面上的元素上移
    location的部分属性
    在ajax请求下的缓存机制
    苹果机的时间格式转换为时间搓
    $.extendGit 丢弃所有本地修改的方法
    调起微信扫一扫
  • 原文地址:https://www.cnblogs.com/rollrock/p/4671914.html
Copyright © 2011-2022 走看看