我的应用程序计算日期和NSDate()之间的天数 . 当我发布它时,用户只能保存一个日期, Headers 和背景图像 .
现在我有一个UICollectionView,可以选择保存多个日期,它会通过将日期, Headers 和图像字符串附加到各自的数组来创建单元格 .
该应用程序已完全更改,因此我正在努力检查用户是否已保存日期,如果有,请将该信息添加到阵列以创建单元格 .
但是,我希望只检查一次 - 第一次从更新或全新安装打开应用程序 .
这是我对它的思考,顺便说一下它不起作用 .
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell
if userDefault.objectForKey("day") == nil {
} else {
// Add the first date created from previous version
let day = userDefault.objectForKey("day") as? String
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd MMMM yyyy hh:mm a"
let date = dateFormatter.dateFromString(day!)!
let date1 = dateFormatter.stringFromDate(date)
addDateToArray(date1)
// Add the since text
let text = userDefault.objectForKey("sinceText") as? String
addSinceLabelToArray(text!)
//Add the image background
let image = userDefault.objectForKey("khoury") as! String
addThemeToImagesArray(image)
}
上面的代码会发生什么,它返回nil . 我期望它创建第一个单元格,以便用户可以看到他们保存的日期 .
2 回答
您可以在
NSUserDefaults
中使用另一个布尔值来检测第一次运行:在视图控制器中创建一个数组 .
然后在
viewDidLoad
:然后重新加载您的集合视图