- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我制作了一个 UITableView,其中的单元格在您点击它们时会展开。它模仿以下项目:https://github.com/rushisangani/TableViewCellExpand
基本上,单元格的容器 View 有两个 subview ,它们充当扩展/收缩状态的容器——“前”和“后”,每个都被限制在单元格的顶部、底部、前导和尾部边缘主要内容 View 。为了使单元格展开或收缩,我只需切换前 View 和后 View 底部约束上的 isActive 属性。这有效,但前提是我在点击时重新加载单元格。如果我只是更改约束,然后尝试调用 invalidateIntrinsicContentSize(),则什么也不会发生。
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
// Expand/contract the cell and invalidate size (doesn't work)
// let cell = tableView.cellForRow(at: indexPath) as! ExpandingCell
// cell.tap()
// cell.invalidateIntrinsicContentSize()
// Keep track of the selected index and configure the expand/contract state when the cell is remade
tableView.deselectRow(at: indexPath, animated: false)
expandedIndexPath = indexPath
if(!expandedIndexPathArray.contains(indexPath)){
expandedIndexPathArray.append(indexPath)
}
else{
expandedIndexPathArray = expandedIndexPathArray.filter({$0 != indexPath})
}
// Whenever a cell's intrinsicContentSize changes, it must be reloaded
tableView.reloadRows(at: [indexPath], with: .none)
}
幕后发生了什么?为什么单元格不能在不重新加载的情况下重新计算其大小?
最佳答案
如果你想根据内容的固有大小收缩/扩展单元格,你需要执行以下 3 个步骤:
配置 TableView :
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44
对于静态 TableView Controller ,您需要实现 heightForRowAtIndexPath
并返回 UITableViewAutomaticDimension
。
更新单元约束以反射(reflect)收缩/展开状态
要求 TableView 反射(reflect)变化:
tableView.beginUpdates()
tableView.endUpdates()
关于swift - 更改 UITableViewCell 的 intrinsicContentSize 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38607718/
所以我最近在创作 Apple calls a leaf-level view (一个按钮)所以我按照 Apple 的文档来实现 -intrinsicContentSize 并且一切正常(尽管代码感觉有
我有一个似乎有效的解决方案,但我想确保它是安全的并按预期使用该工具。我可以将我的问题归结为这种情况——我有一个包含多个垂直堆叠的 UILabel 的 UIView。每个标签都可以填充任意文本,这些文本
当使用自动布局时,调用 intrinsicContentSize 似乎是确定正确适合 View 内容所需的 CGSize 的方法。 但是,此方法仅支持有限数量的现有 UIView。每当我制作自定义 V
除了字面上的实现差异外,这两种方法在概念上有何不同?你为什么偏爱其中之一?等 最佳答案 内在内容大小是一个函数,其值根据(等等)......内容而变化!仅当这是这种 View 时才应该使用它。典型的示
我有一个 UICollectionViewController,它生成具有随机颜色的单元格以用于测试目的。现在 UICollectionViewController 已嵌入到 UIScrollView
正如我们所知,UILabel 具有 intrinsicContentSize,我的困惑是当我们将标签固定到它的 super View 时,例如前导和尾随偏移量为 10,并将 numberOfLines
UIView 是否创建了 intrinsicContentSize? 我创建了一个 UIView contentView。我不给它限制大小: UIView *contentView = [UIView
我正在使用具有以下配置的 UIStackView: let contentView = UIStackView() contentView.distribution = .EqualSpacing c
我试图弄清楚如何最好地创建一个根据给定宽度确定其自高度度的 View 。我想要的行为与垂直 UIStackView 的行为非常相似: 当受其 约束时顶部 , 领先 , 和 尾随 边缘,它应该根据其内容
我尝试使用包含位置的 CGRect 来设置组件的位置。 pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRect
我有一个相当简单的自定义 UITableViewCell 子类,其中包含一个 UISwitch、UIImageView 和 UILabel。所有这些都是单元格 contentView 的 subvie
是否可以根据 intrinsicContentSize 动态计算宽高比来应用此类自动布局约束?在文档中,我发现只有固定比率值的约束。 intrinsicContentSize 的实际高度在我的用例中并
我想使用容器 View 来包含相机预览。我希望相机在中间居中并保持适当的纵横比。 (它是粉红色的,可以很明显地看出它的框架在哪里) 我停留在尝试让相机预览显示的尺寸小于容器的第一步。 我有一个用于相机
我正在尝试创建一个自定义容器 View ,它有一个 UIImageView 和一个多行 UILabel 作为 subview 。为了使 View 与自动布局很好地配合使用,我重写了 intrinsic
我正在尝试模拟一种“请稍候...”UILabel。标签的文本必须定期更新。到目前为止,一切都按预期进行。但是,我需要获取标签的固有内容高度才能定位其容器 View (UIView)。 标签是红色背景的
我制作了一个 UITableView,其中的单元格在您点击它们时会展开。它模仿以下项目:https://github.com/rushisangani/TableViewCellExpand 基本上,
我知道 intrinisicContentSize 是 AutoLayout API 的一部分,但除此之外,我发现它与 -sizeThatFits 方法之间有很多功能重叠。 在什么情况下应该使用一个而
我更新到 Xcode 8 beta 5,现在在继承自 UIView 的类上出现以下错误: Method does not override any method from its superclass
Interface Builder 找不到 intrinsicContentSize的自定义 View 。 对于这个给定的 View 代码, import Foundation import UIKi
我有一个outlineView,我在其中放置了编辑时调整大小的NSTextView(想想大纲应用程序)。我已经完成了大部分工作,但有些行为不一致。 在我的 NSOutlineView 上设置: out
我是一名优秀的程序员,十分优秀!