【发布时间】:2017-07-18 20:46:40
【问题描述】:
目前,我已在 UITableViewCell 的 UITableView 的一个部分中嵌入了 UICollectionViewCell。我知道如何在UITableView 的另一部分动态更改单元格的高度,因为我在另一个UITableViewCell 中有一个UITextView,它根据UITextView 中的文本量动态更改单元格的高度。
我遇到的问题是关于包含UICollectionViewCell 的UITableViewCell。我的UICollectionViewCell 有一行 4 张图片,用户可以使用 UIImagePickerController 通过相机或照片库添加这些图片。
目前据我所知,当生成第五张图片时,UITableViewCell 的高度保持不变,但用户可以像这样在UICollectionViewCell 中水平滚动:
还有我的故事板:
非常不言自明,但如果只有 4 张图片,UITableViewCell 与屏幕截图 1 中的相同,但如果 UICollectionViewCell 的高度发生变化,单元格的高度将动态变化。
我已将UICollectionView 的滚动方向设置为仅垂直。在进一步解释之前,这是我的部分代码:
class TestViewController: UITableViewController, UITextFieldDelegate, UITextViewDelegate, UIImagePickerControllerDelegate
{
override func viewDidLoad()
{
super.viewDidLoad()
....
tableView.estimatedRowHeight = 40.0
tableView.rowHeight = UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
var cell: UITableViewCell = UITableViewCell()
if indexPath.section == 1
{
cell = tableView.dequeueReusableCell(withIdentifier: "TextViewCell", for: indexPath)
let textView: UITextView = UITextView()
textView.isScrollEnabled = false
textView.delegate = self
cell.contentView.addSubview(textView)
}
else if indexPath.section == 4
{
if let imagesCell = tableView.dequeueReusableCell(withIdentifier: "ImagesCell", for: indexPath) as? CustomCollectionViewCell
{
if images_ARRAY.isEmpty == false
{
imagesCell.images_ARRAY = images_ARRAY
imagesCell.awakeFromNib()
}
return imagesCell
}
}
return cell
}
....
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
if indexPath.section == 1
{
return UITableViewAutomaticDimension
}
else if indexPath.section == 4
{
//return 95.0
return UITableViewAutomaticDimension
}
return 43.0
}
....
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
{
if let cell = tableView.cellForRow(at: IndexPath(row: 0, section: 4) ) as? CustomCollectionViewCell
{
cell.images_ARRAY.append(selectedImage)
cell.imagesCollectionView.reloadData()
}
}
picker.dismiss(animated: true, completion: nil)
}
....
func textViewDidChange(_ textView: UITextView)
{
...
// Change cell height dynamically
tableView.beginUpdates()
tableView.endUpdates()
}
}
class CustomCollectionViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
{
@IBOutlet var imagesCollectionView: UICollectionView!
var images_ARRAY = [UIImage]()
var images = [INSPhotoViewable]()
override func awakeFromNib()
{
super.awakeFromNib()
for image in images_ARRAY
{
images.append(INSPhoto(image: image, thumbnailImage: image) )
}
imagesCollectionView.dataSource = self
imagesCollectionView.delegate = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return images_ARRAY.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! ExampleCollectionViewCell
cell.populateWithPhoto(images[(indexPath as NSIndexPath).row]
return cell
}
....
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
return UIEdgeInsetsMake(0.0, 25.0, 0.0, 25.0)
}
}
最初,包含UICollectionViewCell 的indexPath.section == 4 返回高度95,但我将其注释掉并将其替换为返回UITableViewAutomaticDimension。我假设调整了单元格的高度以适合第 5 张图像,但即使 UICollectionViewCell' 高度发生变化,单元格仍保持静态高度,允许我在该静态 UITableViewCell 高度内垂直滚动。
我知道这些是我发现的一些与我的情况非常相似的问题,但它们并没有帮助我解决我的特定问题:
- Swift: Expand UITableViewCell height depending on the size of the UICollectionView inside it
- Auto Height of UICollectionView inside UITableViewCell
- UICollectionView inside a UITableViewCell — dynamic height?
根据一些答案和建议,我添加了以下内容:
imagesCell.images_ARRAY = images_ARRAY
imagesCell.awakeFromNib()
// Added code
imagesCell.frame = tableView.bounds
tableView.setNeedsLayout()
tableView.layoutIfNeeded()
但是,这没有任何影响。谁能指出我需要哪些代码并放置在哪里的正确方向?
谢谢!
【问题讨论】:
标签: ios swift uitableview uicollectionviewcell