根据内容动态设置UIScrollView高度和UITableView高度 [英] Dynamically set UIScrollView height and UITableView Height depending on content

查看:95
本文介绍了根据内容动态设置UIScrollView高度和UITableView高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置:我有一个滚动视图,其中带有标签,uiimage和嵌套的表格视图(分组).标签,uiimage和tableveiw由Web服务填充.分组表格视图的最后一部分包含的文本将永远不会相同,最长可能是5个字符到250+.另外,此视图是从基本的表格视图中推送的(因此它具有导航栏.如果这很重要).

Setup: I have a scrollview with a label, uiimage, and a tableview (grouped) nested within. The label, uiimage, and tableveiw are populated by a webservice. The last section in the grouped table view contains text that will never be the same and could be as long as 5 characters to 250+. Also, this view is pushed from a basic tableview (so it has a navigation bar. If that matters at all).

预期:表格视图的高度应根据从Web服务接收的内容的长度而定.然后设置滚动视图的高度以适应表格视图的高度

Expected: The tableview should extend in height depending on the length of the content received from the web service. Then set the scrollview height to accommodate the height of the tableview

问题:我不太确定如何解决该问题.我真的只知道如何将高度更改为固定值,这在许多情况下将无法正常工作.

Problem: I'm not quite sure how to approach the issue. I really only know how to change the height to fixed values, which will not work properly in many scenarios.

推荐答案

单元格的宽度和高度将被忽略;单元格的大小将根据您从 -tableView:heightForRowAtIndexPath:或(失败)的 tableView.rowHeight 返回的值进行调整.如果单元格中的标签的大小足够大,则可能会好像单元格足够大出现,因为允许标签大于单元格(并在单元格外部渲染).

The width and height of the cell are ignored; the cell cell is re-sized according to the value you return from -tableView:heightForRowAtIndexPath: or (failing that) tableView.rowHeight. It might appear as if the cell is big enough if the label in the cell is sized to be big enough, because the label is allowed to be bigger than (and render outside) the cell.

一种方法是覆盖 -tableView:heightForRowAtIndexPath:以返回正确的高度.实际上,这并不是UITableView的预期用途(它主要是为从同一内容列表动态生成的多个单个高度的行设计的.)

One way is to override -tableView:heightForRowAtIndexPath: to return the correct height. This isn't really the intended use of UITableView (it's primarily designed for lots of rows of a single height, dynamically generated from a list of content).

另一种方法是改为设置 tableView.tableFooterView = myCustomFooter .这可能是最简单的方法.在执行分配之前,请正确调整大小(高度很重要;无论如何,表格视图都会为您设置宽度).另外,请确保未设置自动调整大小标志,否则当表格视图更改大小时(例如在自动旋转时)大小会随机改变.

Another way is to set tableView.tableFooterView = myCustomFooter instead. This is probably the easiest way. Size it correctly before performing the assignment (the height matters; the table view will set the width for you anyway). Also make sure that the autoresizing flags are not set, or the size will appear to randomly change when the table view changes size (e.g. on autorotation).

这篇关于根据内容动态设置UIScrollView高度和UITableView高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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