删除一个单元格时,动态调整大小的UICollectionViewCell行为会发生更改

时间:2017-11-10 23:06:45

标签: ios swift dynamic uicollectionview cell

所以我正在开发这个应用程序,其中有一个带有UITextView的UICollectionViewCell,它根据单元格中的内容大小动态调整大小。我能够通过这个实现这个大小调整:

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

调整大小按预期工作。但是,当我删除特定单元格时,其他单元格的内容会被切断。奇怪的是,我只需要回到之前的VC并返回到UICollectionViewController,整个collectionView就会恢复正常。

我会链接一些图片,以便你们得到一些帮助: 我在这做错了什么。请帮忙!

Dynamically resized cells

Deleted one cell and now the second note last line is cut off

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

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

// MARK: UICollectionViewDataSource

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
}

@objc func deleteUser(sender:UIButton) {
    let i : Int = (sender.layer.value(forKey: "index")) as! Int
    allnotes.remove(at: i)
    addedNotesArrayGlobal.addednotesarrays.remove(at: i)
    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) { self.collectionView?.collectionViewLayout.invalidateLayout() }
    collectionView?.reloadData()

}

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

0 个答案:

没有答案