简体   繁体   中英

Updating uicollectionview when core data object changes

I'm trying to convert a table view to a collection view. I have it display the cells fine but any changes made to them aren't being displayed until i leave the view/come back or scroll the cell off screen. The data comes from core data

The first cell is being update correctly, but the other cells aren't. If i update again, they get updated but only to the value of the previous update. I can see the update function being called but the configure cell isn't being called to actually update the contents, only on the first cell

Full code here: http://pastebin.com/BDaVS6vq

The results of the nslogs give

// View initial load 2016-05-18 15:03:38.012 Calona[1304:315892] 3. Pet: Optional("Pea") - Exp: Optional(2000) 2016-05-18 15:03:38.022 Calona[1304:315892] 3. Pet: Optional("Squi") - Exp: Optional(2000) 2016-05-18 15:03:38.033 Calona[1304:315892] 3. Pet: Optional("Whisper Light") - Exp: Optional(2000)

// After first update 2016-05-18 15:06:12.686 Calona[1309:316804] 1. User Pet: Optional("Pea") 2016-05-18 15:06:12.698 Calona[1309:316804] 2. Update 2016-05-18 15:06:12.705 Calona[1309:316804] 3. Pet: Optional("Pea") - Exp: Optional(4000) 2016-05-18 15:06:12.710 Calona[1309:316804] 3. Pet: Optional("Squi") - Exp: Optional(3000) 2016-05-18 15:06:12.714 Calona[1309:316804] 3. Pet: Optional("Whisper Light") - Exp: Optional(3000) 2016-05-18 15:06:12.725 Calona[1309:316804] 1. User Pet: Optional("Squi") 2016-05-18 15:06:12.732 Calona[1309:316804] 2. Update 2016-05-18 15:06:12.737 Calona[1309:316804] 1. User Pet: Optional("Whisper Light") 2016-05-18 15:06:12.742 Calona[1309:316804] 2. Update 2016-05-18 15:06:12.895 Calona[1309:316804] 2. Update 2016-05-18 15:06:12.895 Calona[1309:316804] 2. Update

After second update 2016-05-18 15:06:26.903 Calona[1309:316804] 1. User Pet: Optional("Pea") 2016-05-18 15:06:26.918 Calona[1309:316804] 2. Update 2016-05-18 15:06:26.924 Calona[1309:316804] 3. Pet: Optional("Pea") - Exp: Optional(5000) 2016-05-18 15:06:26.931 Calona[1309:316804] 3. Pet: Optional("Squi") - Exp: Optional(4000) 2016-05-18 15:06:26.936 Calona[1309:316804] 3. Pet: Optional("Whisper Light") - Exp: Optional(4000) 2016-05-18 15:06:26.945 Calona[1309:316804] 1. User Pet: Optional("Squi") 2016-05-18 15:06:26.952 Calona[1309:316804] 2. Update 2016-05-18 15:06:26.958 Calona[1309:316804] 1. User Pet: Optional("Whisper Light") 2016-05-18 15:06:26.962 Calona[1309:316804] 2. Update 2016-05-18 15:06:27.045 Calona[1309:316804] 2. Update 2016-05-18 15:06:27.047 Calona[1309:316804] 2. Update 2016-05-18 15:06:27.048 Calona[1309:316804] 2. Update

It's possible that are missing these lines in the viewDidLoad of your ViewController :

self.collectionView.dataSource = self;
self.collectionView.delegate = self;

and try to use :

func controllerDidChangeContent(controller: NSFetchedResultsController) {
    self.collectionView.performBatchUpdates({ () -> Void in
        self?.blockOperation.start();
        }, completion: { (success) -> Void in
            ...
    })
}

and I also defined:

func controllerWillChangeContent(controller: NSFetchedResultsController) {
    self.blockOperation = NSBlockOperation();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM