【发布时间】:2019-03-05 23:27:06
【问题描述】:
当您点击 CollectionView 单元格中的关闭按钮时,我想删除核心数据中的一条记录。我在 Collection View Cell 控制器中创建了一个 UIButton,并在 CollectionView Controller 文件中添加了扩展名。
indexPath 给出了一个数字,我创建了 let deleteCellNumber,但我收到了错误:
'无法将'IndexPath.Element(又名'Int')类型的值转换为预期的参数类型'NSManagementObject'
extension soundboardVC: SoundboardCellDelegate {
func delete(cell: soundboardCellVC) {
if let indexPath = collectionView?.indexPath(for: cell) {
let soundRequest:NSFetchRequest<Soundboard> = Soundboard.fetchRequest()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let deleteCellNumber = indexPath[1]
context.delete(deleteCellNumber)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
do {
soundBoardData = try managedObjectContext.fetch(soundRequest)
} catch {
print("Fetching Failed")
}
}
}
}
【问题讨论】:
-
与错误无关,但使用
indexPath.item而不是indexPath[1]
标签: ios swift core-data uicollectionview