UICollectionView中删除一个单元格后刷新单元格布局
Posted
技术标签:
【中文标题】UICollectionView中删除一个单元格后刷新单元格布局【英文标题】:Cell Layout refresh after deleting one cell in UICollectionView 【发布时间】:2017-11-11 07:41:00 【问题描述】:我创建了一个 UICollectionView 动态调整单元格高度。 我正在像这样调整单元格的大小:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
let approximatedwidthoftextview = view.frame.width - 70
let size = CGSize(width: approximatedwidthoftextview, height: 1000)
let attributes = [NSAttributedStringKey.font: UIFont(name: "Avenir Next", size: 18)]
let estimatedFrame = NSString(string: allnotes[indexPath.row].note).boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attributes, context: nil)
return CGSize(width: view.frame.width, height: estimatedFrame.height + 38)
当我删除一个单元格时,剩余单元格的布局会有点奇怪。内容被截断。
奇怪的是,导航到前一个视图控制器,然后返回到这个控制器修复了单元格布局,单元格恢复正常。
知道这里有什么问题吗?
import UIKit
private let reuseIdentifier = "Cell"
class AddedNotesCollectionViewController: UICollectionViewController,
UICollectionViewDelegateFlowLayout
var allnotes = [notesarray]()
override func viewDidAppear(_ animated: Bool)
print(allnotes.count)
override func viewDidLoad()
super.viewDidLoad()
view.backgroundColor = UIColor(red:0.15, green:0.20, blue:0.22, alpha:1.0)
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Register cell classes
self.collectionView!.register(AddedNotesCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
// Do any additional setup after loading the view.
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
// #warning Incomplete implementation, return the number of items
return allnotes.count
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
var tag = indexPath.row
print(tag)
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! AddedNotesCollectionViewCell
// Configure the cell
cell.deleteBtn.layer.setValue(indexPath.row, forKey: "index")
cell.deleteBtn.addTarget(self, action: #selector(deleteUser(sender:)), for: UIControlEvents.touchUpInside)
cell.backgroundColor = allnotes[indexPath.row].prioritycolor
cell.textview.text = allnotes[indexPath.row].note
// Remove the button from the first cell
return cell
//deleting cell func
@objc func deleteUser(sender:UIButton)
let i : Int = (sender.layer.value(forKey: "index")) as! Int
allnotes.remove(at: i)
addedNotesArrayGlobal.addednotesarrays.remove(at: i)
collectionView?.reloadData()
override func viewDidLayoutSubviews()
super.viewDidLayoutSubviews()
collectionView?.collectionViewLayout.invalidateLayout()
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
let approximatedwidthoftextview = view.frame.width - 70
let size = CGSize(width: approximatedwidthoftextview, height: 1000)
let attributes = [NSAttributedStringKey.font: UIFont(name: "Avenir Next", size: 18)]
let estimatedFrame = NSString(string: allnotes[indexPath.row].note).boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attributes, context: nil)
return CGSize(width: view.frame.width, height: estimatedFrame.height + 32)
【问题讨论】:
您需要在 viewDidLayoutSubviews() 中为布局更改编写代码。删除单元格后,该方法会自动调用,如果没有,则需要调用。 @Jacky 你能详细说明一下吗?你说的是哪个代码?? @Jacky 对什么是“布局更改代码”感到困惑?无效布局()? 您需要为集合视图布局编写代码。 @Jacky Proper 很困惑。这就是为什么-> 我正在更改“sizeforitematindexPath”中的单元格高度,仅此而已。你说的是哪个collectionViewLayout代码? 【参考方案1】:尽管使用上述算法在collectionView 中动态调整大小效果很好,但它只是不完整。我切换到 UITableView 并使用自动布局进行动态调整大小。它简单、容易而且效果很好。
【讨论】:
以上是关于UICollectionView中删除一个单元格后刷新单元格布局的主要内容,如果未能解决你的问题,请参考以下文章
删除单元格后,collectionViewCell中的按钮中的索引错误