从UICollectionViewCell

时间:2018-02-17 23:17:35

标签: ios swift core-data uicollectionview

目前我的核心数据显示在UICollectionView单元格中。我试图删除特定单元格上的核心数据条目。因此用户从UICollectionViewCells转到特定的单元格。我希望用户点击一个按钮,删除该特定单元格的coreData条目。我已经看到这在网上查看了很多,但没有使用collectionViewCell。

的CollectionView

import UIKit
import CoreData

class whyCollectionViewCell: UICollectionViewCell {
    @IBOutlet var general: UILabel!
    @IBOutlet var blockbuster: UIImageView!

    @IBAction func judo(_ sender: Any) {
                        if cdHandler.deletObject(user: users[1]){
                            users = cdHandler.fetchObject()!
                        }

    }
}

class collectionVIEW: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{
    var users = [User]()
    @IBOutlet var theIssues: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        users = cdHandler.fetchObject()!
        theIssues.reloadData()
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return users.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! whyCollectionViewCell
        cell.general.text = users[indexPath.row].userName

        if let imageData = users[indexPath.row].pic {
            let coredataLoadedimage = UIImage(data: imageData)
            cell.blockbuster.image = coredataLoadedimage
        }
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let cell = collectionView.cellForItem(at: indexPath) as? whyCollectionViewCell {
            UIView.animate(withDuration: 1, delay: 0.1, usingSpringWithDamping: 2, initialSpringVelocity: 2, animations: {
                cell.frame = self.theIssues.bounds
                self.theIssues.isScrollEnabled = true
                cell.superview?.bringSubview(toFront: cell)
            }, completion: nil)
        }
    }
}

核心数据

    import UIKit
import CoreData
class cdHandler: NSObject {

    private class func getContext() -> NSManagedObjectContext {
        let appdeleagetzz = UIApplication.shared.delegate as! AppDelegate
        return appdeleagetzz.persistentContainer.viewContext
    }
    class func saveObject(pic: Data, userName: String) -> Bool {
        let context = getContext()
        let entity = NSEntityDescription.entity(forEntityName: "User", in: context)
        let managedObject = NSManagedObject(entity: entity!, insertInto: context)
        managedObject.setValue(pic, forKey:"pic")
                    managedObject.setValue(userName, forKey:"userName")
        do {
            try context.save()
            return true

        } catch {
            return false

        }
    }
    class func deletObject(user: User) -> Bool {
        let context = getContext()
        context.delete(user)

        do {
            try context.save()
            return true
        } catch {
            return false

        }
    }

    class func fetchObject() -> [User]? {

        do {
            let context = getContext()
            return try context.fetch(User.fetchRequest())
        } catch {
            return [User]()
        }
    }

}

1 个答案:

答案 0 :(得分:0)

你可以做一个像这样的功能:

func removeUser(at: Int) {
    // You should create a function that removes from core data
    // And use a completion handler. Not just wait for return.
    deleteFromCoreData(with: {
        users.remove(at: at)
        collectionView.deleteItems(at: [IndexPath(row: at, section: 0)])
    }
}

你可以制作一个单元格类,然后制作一个像这样的委托协议:

protocol CellWithDeleteButtonDelegate: AnyObject {
    // AnyObject to support being placed in a weak var
    func cellWithDeleteButtonDeletePressed(_ cellWithDeleteButton: CellWithDeleteButton, at id: Int)
}

在细胞类中:

weak var delegate: CellWithDeleteButtonDelegate? // Weak to avoid memory leaks (retain cycles)
var id: Int?

然后在cellForRowAt

     (cell as? CellWithDeleteButton)?.delegate = self
     (cell as? CellWithDeleteButton)?.id = indexPath.row

让你的视图控制器采用CellWithDelegateButtonDelegate,并实现调用removeUser(at: id)的方法