在 UICollectionView 中删除一个单元格后单元格布局刷新 [英] Cell Layout refresh after deleting one cell in UICollectionView

查看:142
本文介绍了在 UICollectionView 中删除一个单元格后单元格布局刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 UICollectionView,可以动态调整单元格高度的大小.我正在像这样调整单元格的大小:

I created a UICollectionView with dynamic resizing of cell height. I am resizing the cell like this:

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)

}

当我删除一个单元格时,剩余单元格的布局变得有点奇怪.内容被切断.

When I delete one cell, the remaining cell’s layout get a bit strange. The content gets cut off.

奇怪的是,导航到前一个视图控制器然后返回到这个控制器修复了单元格布局,单元格恢复正常.

Strangely enough, navigating to a previous viewcontroller and then coming back to this controller fixes the cell layout and cells go back to normal.

知道这里出了什么问题吗?

Any idea what’s wrong here?

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)
}

}

推荐答案

尽管使用上述算法在 collectionView 中动态调整大小效果很好,但它并不完整.我切换到 UITableView 并使用自动布局来动态调整大小.这很简单,容易并且工作得很好.

Even though dynamic resizing works well in collectionView using the above algorithm, it's just not complete. I switched to UITableView and used auto-layouts to do dynamic resizing. It's simple, easy and works quite well.

这篇关于在 UICollectionView 中删除一个单元格后单元格布局刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆