向左滑动 uitableViewCell 时将 tableView Item 保存为默认值
Save tableViewItem in defaults when tableViewCell is swiped left
我按照 的回答在向左滑动并单击收藏夹按钮时将我的数据保存在数组中。我目前做的是这个
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let favorite = UITableViewRowAction(style: .normal, title: "Favorite") { (action, indexPath) in
var favorites : [String] = []
let defaults = UserDefaults.standard
if let favoritesDefaults : AnyObject? = defaults.object(forKey: "favorites") as AnyObject {
favorites = favoritesDefaults! as! [String]
}
let cell = self.myTableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! TableViewCell
favorites.append(itemList[indexPath.row])
defaults.set(favorites, forKey: "favorites")
defaults.synchronize()
}
return [favorite]
}
数组列表
var itemList = [ "item1", "item2", "item3", "item4", "item5",
"item6", "item7", "item8", "item" , "item", "Gobbling"]
当我点击最喜欢的按钮时出现错误
Could not cast value of type 'NSNull' (0x22e386f28) to 'NSArray' (0x22e386960)
不要将 cellForRowAt
之外的单元格出队。永远不要那样做。无论如何,该单元格未被使用。
使用专用的 API array(forKey
从 UserDefaults
读取数组并将类型转换为预期类型而不是未指定的 Any(Object)
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let favorite = UITableViewRowAction(style: .normal, title: "Favorite") { [unowned self] (action, indexPath) in
let defaults = UserDefaults.standard
var favorites = defaults.array(forKey: "favorites") as? [String] ?? []
favorites.append(self.itemList[indexPath.row])
defaults.set(favorites, forKey: "favorites")
}
return [favorite]
}
我按照
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let favorite = UITableViewRowAction(style: .normal, title: "Favorite") { (action, indexPath) in
var favorites : [String] = []
let defaults = UserDefaults.standard
if let favoritesDefaults : AnyObject? = defaults.object(forKey: "favorites") as AnyObject {
favorites = favoritesDefaults! as! [String]
}
let cell = self.myTableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! TableViewCell
favorites.append(itemList[indexPath.row])
defaults.set(favorites, forKey: "favorites")
defaults.synchronize()
}
return [favorite]
}
数组列表
var itemList = [ "item1", "item2", "item3", "item4", "item5",
"item6", "item7", "item8", "item" , "item", "Gobbling"]
当我点击最喜欢的按钮时出现错误
Could not cast value of type 'NSNull' (0x22e386f28) to 'NSArray' (0x22e386960)
不要将 cellForRowAt
之外的单元格出队。永远不要那样做。无论如何,该单元格未被使用。
使用专用的 API array(forKey
从 UserDefaults
读取数组并将类型转换为预期类型而不是未指定的 Any(Object)
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let favorite = UITableViewRowAction(style: .normal, title: "Favorite") { [unowned self] (action, indexPath) in
let defaults = UserDefaults.standard
var favorites = defaults.array(forKey: "favorites") as? [String] ?? []
favorites.append(self.itemList[indexPath.row])
defaults.set(favorites, forKey: "favorites")
}
return [favorite]
}