是否可以在今天的扩展中使用UICollectionView?

时间:2016-08-28 00:55:18

标签: swift uicollectionview today-extension

我在主应用程序中完成的代码完全相同,但我在通知中心得到的只是一张黑色的图像?

也许你不能在今天的扩展中使用uicollectionview?

感谢您的帮助

这是视图控制器文件,我不确定什么是有用的,什么不是,所以我已经包含了大部分文件。

import UIKit
import NotificationCenter

class TodayViewController: UIViewController, NCWidgetProviding,     UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate {
@IBOutlet weak var collectionView: UICollectionView!

var datesArray: [String] = []
var sinceLabelArray: [String] = []
let dateFormatter = NSDateFormatter()

override func viewDidLoad() {
    super.viewDidLoad()

    self.datesArray = NSUserDefaults.standardUserDefaults().objectForKey("saveddates") as? [String] ?? [String]()

    self.sinceLabelArray = NSUserDefaults.standardUserDefaults().objectForKey("savedsincelabels") as? [String] ?? [String]()

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

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

let reuseIdentifier = "cell" // also enter this string as the cell identifier in the storyboard

// tell the collection view how many cells to make
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return self.datesArray.count
}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int   {
    return 1
}

override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {

    self.collectionView.reloadData()

}

// make a cell for each cell index path
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! Cell

    cell.dayCount.text = dayCount(self.datesArray[indexPath.item])
    cell.sinceOrUntilLabel.text = daysSinceOrUntil(self.datesArray[indexPath.item])
    cell.eventLabel.text = self.sinceLabelArray[indexPath.item]

    return cell
}

// For dayCountLabel.text
func dayCount (dayCount: String) -> String{
    let day = dayCount
    let date2 = NSDate()
    //let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "dd MMMM yyyy hh:mm a"
    let date1: NSDate = dateFormatter.dateFromString(day)!
    let diffDateComponents = NSCalendar.currentCalendar().components([NSCalendarUnit.Day], fromDate: date1, toDate: date2, options: NSCalendarOptions.init(rawValue: 0))
    let difference = String("\(abs(diffDateComponents.day))")
    return difference
}

// For daySinceOrUntilLabel.text
func daysSinceOrUntil (daySince: String) -> String {
    let day = daySince
    let date2 = NSDate()
    //let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "dd MMMM yyyy hh:mm a"
    let date1: NSDate = dateFormatter.dateFromString(day)!
    let diffDateComponents = NSCalendar.currentCalendar().components([NSCalendarUnit.Day], fromDate: date1, toDate: date2, options: NSCalendarOptions.init(rawValue: 0))

    if date1.compare(date2) == NSComparisonResult.OrderedDescending  {
        let text: String = "Days Until"
        return text
    } else if date1.compare(date2) == NSComparisonResult.OrderedAscending {
        let text: String = "Days Since"
        return text
    } else {
        return "Days"
    }
}



func collectionView(collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                           sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    //let kWhateverHeightYouWant = 200
    return CGSizeMake(collectionView.bounds.size.width, collectionView.bounds.size.height)
}


func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
    self.collectionView.reloadData()

    completionHandler(NCUpdateResult.NewData)
}

}

0 个答案:

没有答案