使用 NSUserDefaults 不会打印 Tableview 数据
Tableview data won't print using NSUserDefaults
我试图在 NSUserDefaults 中获取我保存的数组并将其添加到我的表视图中...当我 运行 print(notifications[indexPath.row])
我的数组(通知)内容被打印时。但是,当我将这些内容添加到我的表格视图时,什么也没有显示。我假设虽然数组通过 NSUserDefaults 保存和检索,但它可能无法将数组内容转换为可以添加到我的表视图中的字符串。任何帮助,将不胜感激。谢谢!
下面是一些可能有用的额外代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = notificationsTableView.dequeueReusableCellWithIdentifier("notificationsCell") as! NotificationsTableViewCell
//Set notificationsLabel of cells
print(notifications[indexPath.row])
cell.notificationsLabel.text = notifications[indexPath.row] as? String
//Set datesLabel of cells
cell.datesLabel.text = dates[indexPath.row] as? String
return cell
}
修复:我必须指定数组只由 String
组成。为此,我通过 var notifications = [NSString]()
初始化了数组。然后我可以使用 append
命令来编辑该数组。最后,将保存的内容添加到我运行、notifications = NSUserDefaults.standardUserDefaults().objectForKey("notificationsKey") as! [NSString]
的数组中。
我试图在 NSUserDefaults 中获取我保存的数组并将其添加到我的表视图中...当我 运行 print(notifications[indexPath.row])
我的数组(通知)内容被打印时。但是,当我将这些内容添加到我的表格视图时,什么也没有显示。我假设虽然数组通过 NSUserDefaults 保存和检索,但它可能无法将数组内容转换为可以添加到我的表视图中的字符串。任何帮助,将不胜感激。谢谢!
下面是一些可能有用的额外代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = notificationsTableView.dequeueReusableCellWithIdentifier("notificationsCell") as! NotificationsTableViewCell
//Set notificationsLabel of cells
print(notifications[indexPath.row])
cell.notificationsLabel.text = notifications[indexPath.row] as? String
//Set datesLabel of cells
cell.datesLabel.text = dates[indexPath.row] as? String
return cell
}
修复:我必须指定数组只由 String
组成。为此,我通过 var notifications = [NSString]()
初始化了数组。然后我可以使用 append
命令来编辑该数组。最后,将保存的内容添加到我运行、notifications = NSUserDefaults.standardUserDefaults().objectForKey("notificationsKey") as! [NSString]
的数组中。