gpt4 book ai didi

ios - 如何从 UITableViewController 中删除行

转载 作者:行者123 更新时间:2023-11-28 13:06:29 26 4
gpt4 key购买 nike

我有 UITableViewController。它具有来自文档目录中文件数组的数据。我想用这个方法删除行 self.navigationItem.leftBarButtonItem = self.editButtonItem()
但我做到了,文件被删除但行没有消失。我尝试了几种方法,但它们没有帮助我。我用 CoreData 的数据做了很多次。但我不知道如何做到这一点。请帮助我。

    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
tableView.beginUpdates()
let currentSong = listOfMP3Files[indexPath.row]
println(currentSong)
let directory = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).first as! NSURL
let url = directory.URLByAppendingPathComponent(currentSong)
// println(url)
// println(indexPath)
fileManager.removeItemAtURL(url, error: nil)
tableView.beginUpdates()
tableView.deleteRowsAtIndexPaths(NSArray(object: indexPath) as [AnyObject], withRowAnimation: UITableViewRowAnimation.Automatic)
tableView.endUpdates()
} else if editingStyle == .Insert {

}
}

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case NSFetchedResultsChangeType.Insert:
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: UITableViewRowAnimation.Fade)
break
case NSFetchedResultsChangeType.Delete:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Fade)
break
case NSFetchedResultsChangeType.Move:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Fade)
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: UITableViewRowAnimation.Fade)
break
case NSFetchedResultsChangeType.Update:
tableView.cellForRowAtIndexPath(indexPath!)
default: break
}
}

func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
switch type {
case NSFetchedResultsChangeType.Insert:
tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: UITableViewRowAnimation.Fade)
case NSFetchedResultsChangeType.Delete:
tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: UITableViewRowAnimation.Fade)
case NSFetchedResultsChangeType.Move:
tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: UITableViewRowAnimation.Fade)
tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: UITableViewRowAnimation.Fade)
case NSFetchedResultsChangeType.Update:
break
default: break
}
}

func controllerDidChangeContent(controller: NSFetchedResultsController) {
tableView.endUpdates()
}

func controllerWillChangeContent(controller: NSFetchedResultsController) {
tableView.beginUpdates()
}

第二种变体

    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
tableView.beginUpdates()
let currentSong = listOfMP3Files[indexPath.row]
println(currentSong)
let directory = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).first as! NSURL
let url = directory.URLByAppendingPathComponent(currentSong)
// println(url)
// println(indexPath)
fileManager.removeItemAtURL(url, error: nil)
tableView.beginUpdates()
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
tableView.endUpdates()
} else if editingStyle == .Insert {

}
}

最佳答案

您缺少调用 removeAtIndex() 的行,它从数据源数组中删除指定索引处的项目

tableView.beginUpdates()
listOfMP3Files.removeAtIndex(indexPath.row) //Add this line
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
tableView.endUpdates()

关于ios - 如何从 UITableViewController 中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32602923/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com