Ios UITableViewCell动态行高+;自定义UIView+;自动布局
我正在尝试获取一个带有多个Ios UITableViewCell动态行高+;自定义UIView+;自动布局,ios,objective-c,swift,xcode,uitableview,Ios,Objective C,Swift,Xcode,Uitableview,我正在尝试获取一个带有多个uiimageview的自定义照片容器,以适合我的tableview单元格。视图包含数量可变的图像(1~9),其高度将相应地从1x变为3x imageHeight 我使用AutoLayout来定义包含tableview和自定义UIView的上/下/前导/尾随页边距,并启用自调整单元格大小,我设置了 tableView.estimatedRowHeight = X tableView.rowHeight = UITableViewAutomaticDimension 我
uiimageview
的自定义照片容器,以适合我的tableview单元格。视图包含数量可变的图像(1~9),其高度将相应地从1x变为3x imageHeight
我使用AutoLayout来定义包含tableview和自定义UIView的上/下/前导/尾随页边距,并启用自调整单元格大小,我设置了
tableView.estimatedRowHeight = X
tableView.rowHeight = UITableViewAutomaticDimension
我用
tableView.register(nib: forCellReuseIdentifier:)
在tableView(tableView:cellForRowAt:)
方法中,我使用以下设置单元格:
let cell = tableView.dequeueReusableCell(
withIdentifier: "test9cell",
for: indexPath) as! SocialFeedTableViewCell
cell.photoContainer.setup(with: urls)
cell.photoContainer.loadImages()
return cell
其中setup()
使用URL钩住容器中的每个imageView
func setup(with urls: [URL]) {
self.imageUrls = urls
for i in 0 ..< urls.count {
let imageView = UIImageView(frame: CGRect.zero)
self.addSubview(imageView)
self.imageViews.append(imageView)
}
self.setNeedsLayout()
}
func loadImages() {
self.imageViews.forEach { imageView in
imageView.frame = // Calculate position for each subview
imageView.sd_setImage(...) // Load web image asynchronously
}
}
这里的问题是:在设置初始intrinsicContentSize之后,容器的帧大小在layoutSubviews()中会随之改变。虽然到那时我可以正确定位imageView子视图,但单元格高度将不再更改
希望我没有让这个问题变得更加混乱。有人能指出在修改UIView子视图的内容后,如何调整单元格高度吗?谢谢 这里有一些事情会导致您在动态调整单元格大小时出现一些问题
您可能还想考虑使用集合视图或将图像合并为单个图像并在单元格中使用该图像。您可以在运行时或服务器端在设备上执行此操作,前提是您可以访问映像
这里有一些事情会导致您在动态调整单元格大小时出现一些问题您可能还想考虑使用集合视图或将图像合并为单个图像并在单元格中使用该图像。您可以在运行时或服务器端在设备上执行此操作,前提是您可以访问映像
override var intrinsicContentSize {
let frameWidth = self.frame.size.width
var frameHeight: CGFloat
switch self.imageUrls.count { // range from 1...9
case 1...3:
frameHeight = frameWidth / 3
case 4...6:
frameHeight = frameWidth / 3 * 2
default:
frameHeight = frameWidth
return CGSize(frameWidth, frameHeight)
}
override func layoutSubviews() {
super.layoutSubviews()
self.imageViews.forEach { imageView in
imageView.frame = // Calculate position for each subview
}
}