- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在重写我的应用程序以使用自动布局。但我坚持一个具体问题。主屏幕上是带有动态内容的表格 View 。每个单元格的顶部是方形照片(左右对齐 20px)。基本上是这样的:
http://cl.ly/image/0N2w2g1r0w11
在真正的应用程序中,单元格中当然会有更多 View ,但即使是这种基本布局我也无法工作。主要问题是,我想根据单元格宽度指定正方形 View 宽度,并且此 View 必须始终为正方形。单元格高度取决于此方形 View 的高度。看起来很简单,但我尝试了很多约束配置(在 IB 中或以编程方式),但总是失败。这是单元格高度的方法:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
[self.prototypeCell congigureCell];
[self.prototypeCell setNeedsLayout];
[self.prototypeCell layoutIfNeeded];
CGSize size = [self.prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height + 1.0;
}
当方形 View 宽度指定为常量时,一切正常。但这不是我想要的(谁知道什么屏幕尺寸会有新的 iPhone,所以我要做好准备:))。我在自动布局方面不是很有经验,所以我希望我只是遗漏了一些明显的东西。我将非常感谢您的每一条建议。
最佳答案
嗯,简短的回答,你总是可以知道任何设备上的屏幕宽度
[[UIScreen mainScreen] bounds].size.width;
因此您可以使用它来计算您的平方,从而计算您的单元格的高度。
现在是更复杂但通用的答案..
您可以使用自动布局来调整所有单元格的大小,只需少量的魔法。第 1 步子类化 UITableViewCell(你可能已经这样做了)并添加 layoutSubviews 方法。
- (void)layoutSubviews
{
[super layoutSubviews];
[self.contentView layoutIfNeeded];
// any other layout stuff needed, multiline labels etc
}
第 2 步:创建一个虚拟单元格。这样做的原因是,要计算单元格的高度,您需要单元格,通常您会调用 tableVeiw:cellForRowAtIndexPath: 来执行此操作。问题是这会调用 tableView:heightForRowAtIndexPath: 反过来又需要单元格所以调用 cellForRowAtIndexPath .. 你看我要去哪里..所以在你的 tableViewController 中
@property (nonatomic, strong) UITableViewCell *dummyCell;
- (UITableViewCell *)dummyCell
{
if (!_dummyCell)
{
// have to dequeue if you are using a storyboard cell.
_dummyCell = [self.tableView dequeueReusableCellWithIdentifier:kCellId];
}
return _dummyCell;
}
第 3 步:对于您的简单方形案例而言并非绝对必要,但为了完整起见包括在内,以防您想要进行更复杂的布局。在 tableViewController 中创建配置单元格方法。
- (void)configureCell:(UITableViewCellCell *)cell withModel:(id)modelObject
{
// some custom setup using model data (labels images etc)
}
第 4 步:现在我们在 tableView:heightForRowAtIndexPath *注意模型是您创建的一些自定义数据对象
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// get model
Model *modelData = self.someDataArray[indexPath.row];
//Configure
[self configureCell:self.dummyCell withModel:modelData];
// layout cell using width of tableview
self.dummyCell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), 0);
[self.offscreenCell setNeedsLayout];
[self.offscreenCell layoutIfNeeded];
// calculate the size the cell need to draw its contents.
// this is where the magic happens!
CGSize size = [self.dummyCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
//return the height of your cell :)
return size.height;
}
就是这样。这可用于计算具有任何 View 的单元格的高度,或者假设您正确使用 layoutSubviews 和 configureCell 的动态大小标签。享受:)
关于ios - 具有动态高度和自动布局的 UITableViewCell - 单元格中的方形 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25495123/
这是代码片段。 请说出这种用小内存存储大数据的算法是什么。 public static void main(String[] args) { long longValue = 21474836
所以我使用 imap 从 gmail 和 outlook 接收电子邮件。 Gmail 像这样编码 =?UTF-8?B?UmU6IM69zq3OvyDOtc68zrHOuc67IG5ldyBlbWFpb
很久以前就学会了 C 代码;想用 Scheme 尝试一些新的和不同的东西。我正在尝试制作一个接受两个参数并返回两者中较大者的过程,例如 (define (larger x y) (if (> x
Azure 恢复服务保管库有两个备份配置选项 - LRS 与 GRS 这是一个有关 Azure 恢复服务保管库的问题。 当其驻留区域发生故障时,如何处理启用异地冗余的恢复服务保管库?如果未为恢复服务启
说,我有以下实体: @Entity public class A { @Id @GeneratedValue private Long id; @Embedded private
我有下一个问题。 我有下一个标准: criteria.add(Restrictions.in("entity.otherEntity", getOtherEntitiesList())); 如果我的
如果这是任何类型的重复,我会提前申请,但我找不到任何可以解决我的具体问题的内容。 这是我的程序: import java.util.Random; public class CarnivalGame{
我目前正在使用golang创建一个聚合管道,在其中使用“$ or”运算符查询文档。 结果是一堆需要分组的未分组文档,这样我就可以进入下一阶段,找到两个数据集之间的交集。 然后将其用于在单独的集合中进行
是否可以在正则表达式中创建 OR 条件。 我正在尝试查找包含此类模式的文件名列表的匹配项 第一个案例 xxxxx-hello.file 或者案例二 xxxx-hello-unasigned.file
该程序只是在用户输入行数时创建菱形的形状,因此它有 6 个 for 循环; 3 个循环创建第一个三角形,3 个循环创建另一个三角形,通过这 2 个三角形和 6 个循环,我们得到了一个菱形,这是整个程序
我有一个像这样的查询字符串 www.google.com?Department=Education & Finance&Department=Health 我有这些 li 标签,它们的查询字符串是这样
我有一个带有静态构造函数的类,我用它来读取 app.config 值。如何使用不同的配置值对类进行单元测试。我正在考虑在不同的应用程序域中运行每个测试,这样我就可以为每个测试执行静态构造函数 - 但我
我正在寻找一个可以容纳多个键的容器,如果我为其中一个键值输入保留值(例如 0),它会被视为“或”搜索。 map, int > myContainer; myContainer.insert(make_
我正在为 Web 应用程序创建数据库,并正在寻找一些建议来对可能具有多种类型的单个实体进行建模,每种类型具有不同的属性。 作为示例,假设我想为“数据源”对象创建一个关系模型。所有数据源都会有一些共享属
(1) =>CREATE TABLE T1(id BIGSERIAL PRIMARY KEY, name TEXT); CREATE TABLE (2) =>INSERT INTO T1 (name)
我不确定在使用别名时如何解决不明确的列引用。 假设有两个表,a 和 b,它们都有一个 name 列。如果我加入这两个表并为结果添加别名,我不知道如何为这两个表引用 name 列。我已经尝试了一些变体,
我的查询是: select * from table where id IN (1,5,4,3,2) 我想要的与这个顺序完全相同,不是从1...5,而是从1,5,4,3,2。我怎样才能做到这一点? 最
我正在使用 C# 代码执行动态生成的 MySQL 查询。抛出异常: CREATE TABLE dump ("@employee_OID" VARCHAR(50)); "{"You have an er
我有日期 2016-03-30T23:59:59.000000+0000。我可以知道它的格式是什么吗?因为如果我使用 yyyy-MM-dd'T'HH:mm:ss.SSS,它会抛出异常 最佳答案 Sim
我有一个示例模式,它的 SQL Fiddle 如下: http://sqlfiddle.com/#!2/6816b/2 这个 fiddle 只是根据 where 子句中的条件查询示例数据库,如下所示:
我是一名优秀的程序员,十分优秀!