如何动态地设置UITextView的高度自定义的UITableViewCell与自动布局内 [英] how to set UITextView height dynamically inside a custom UITableViewCell with autolayout

查看:285
本文介绍了如何动态地设置UITextView的高度自定义的UITableViewCell与自动布局内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的UITableView,每tableViewCell是定制的。里面我customTableViewCell是一个UITextView,TextViews帧针或同它的父是tableViewCell。

我要怎么设置的UITableViewCell高度动态是成正比的TextViews尺寸也将依赖于我从互联网上获取内容的文本。

(样品原型)

//我这是怎么操作的每一个细胞高度

   - (CGFloat的)的tableView:(UITableView的*)的tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //我设置高度这里,什么这样做的最好的方法怎么办。与自动版式BTW
} - (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//我在这里初始化每一个细胞都与我ArrayContainer,没有什么在这里提及。
}


解决方案

  1. 按照这样的回答:链接引用


  2. 一个高度约束添加到您的UITextView并创建一个插座,您的自定义单元格类。


  3. 使用 sizeThatFits:的UITextView 来获取适合内容的大小和设置这个值一个 2中描述的约束在做到这一点的tableView:heightForRowAtIndexPath:


一个需要注意的是,如果有是许多细胞(数百或上千),那么你可能会碰到性能问题。

I have UITableView, every tableViewCell is custom. Inside my customTableViewCell is a UITextView, TextViews frame is pin or same as its superView which is tableViewCell.

How Am I gonna set the UITableViewCell height dynamically that is proportional to the TextViews size which will also depends to content text that I get from internet.

(sample prototype)

// this is how I manipulate every cell height

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    // How do I set the height here, whats the best approach for this. with autolayout BTW
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// I initialize every cell here with my ArrayContainer, nothing much to refer here.
}

解决方案

  1. Follow this answer: Link reference

  2. Add a height constraint to your UITextView and create an outlet to your custom cell class.

  3. Use sizeThatFits: on UITextView to get the size that fits the content and set this value as a constant of the constraint described in 2. Do this in tableView:heightForRowAtIndexPath:.

One caveat is that if there're many cells (hundreds or thousands) then you may hit performance issues.

这篇关于如何动态地设置UITextView的高度自定义的UITableViewCell与自动布局内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆