所以我已经阅读了有关标题中的错误的其他问题,我已经尝试过他们的解决方案,但它们对我不起作用。
在viewDidLoad()
中,我有两个计时器:
dayCountTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(DateMenuViewController.dayCount(_:)), userInfo: nil, repeats: true)
daySinceTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(DateMenuViewController.daysSinceOrUntil(_:)), userInfo: nil, repeats: true)
他们称这两种方法为:
// For dayCountLabel.text
func dayCount (dayCount: String) -> String{
let day = dayCount
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 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"
}
}
以下列方式在UICollectionViewCell
内使用。
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
cell.dayCountLabel.text = dayCount(Globals.datesArray[indexPath.item])
cell.daySinceOrUntilLabel.text = daysSinceOrUntil(Globals.datesArray[indexPath.item])
我在这里做错了什么? 感谢