UICollectionView中的ReloadData

时间:2016-08-22 16:18:21

标签: swift core-data uicollectionview reloaddata

我使用CoreData并希望在我在CoreData中保存新条目时重新加载UICollectionView但是唯一的self.collectionview.reloaddata()没有运行它重新加载任何东西。

感谢您的帮助。

我的CoreData和UICollectionView我已经尝试在clockblogalarm成为新条目时重新加载UICollectionView:

var clockblogalarm = [ClockBlogAlarm] () {
    didSet {              //self.ClockCollectionView.reloadData()
    }
}


var mgdContext = (UIApplication.shared.delegate as! AppDelegate).managedObjectContext


func loadClockAlarms () {
    let LoadRequestBlogAlarm: NSFetchRequest<ClockBlogAlarm> = ClockBlogAlarm.fetchRequest()
    clockblogalarm = try! mgdContext.fetch(LoadRequestBlogAlarm as! NSFetchRequest<NSFetchRequestResult>) as! [ClockBlogAlarm]
}



@IBOutlet var ClockCollectionView: UICollectionView!


override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)    
    self.loadClockAlarms()  
}

这里我将条目保存在CoreData中。当我单击Button Done view.bottomButtonHandler{}时,我想重新加载数据并使用loadClockAlarms()在UICollectionView中显示它。

else if index == 1 {

        let view = AddClockView.instantiateFromNib()
        let window = UIApplication.shared.delegate?.window!
        view.closeButtonHandler = {[weak modal] in
            modal?.closeWithLeansRandom()
            return
        }
        view.bottomButtonHandler = {[weak modal] in
            modal?.closeWithLeansRandom()

            self.loadClockAlarms()


            return
        }

        modal.show(modalView: view, inView: window!)

    }






// MARK: UICollectionViewDataSource



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



override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AlarmBlogCell", for: indexPath) as! AlarmBlogCell

    cell.layer.cornerRadius = 8



    let clockblogalarms = clockblogalarm[indexPath.row]


    if clockblogalarms.lock == "ClockblogAlarm" {

        cell.Title.text = clockblogalarms.title
        cell.Time.text = "SSSSS"

    }




    return cell
}

2 个答案:

答案 0 :(得分:1)

迅捷4.2:

 DispatchQueue.main.async {
  self.collectionView.reloadData()
 }

答案 1 :(得分:0)

确保在主线程中调用reloadData(),因为它是UI负责的操作:

dispatch_async(dispatch_get_main_queue()) {
    self.ClockCollectionView.reloadData()
}