gpt4 book ai didi

swift - 从节中移动最后一行时如何删除节标题

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

我有一个连接到核心数据的 UITableView。表格 View 具有动态单元格并将行分组为部分(类别)。

我可以通过在各部分之间拖动来移动行,这对于核心数据来说效果很好,除非我拖动一行并且该部分变空。我希望节标题消失,但它保留为标题,内部没有行。然后,如果我尝试向后拖动一行,则会收到错误。

我已经为 didChangeObject 和 didChangeSection 实现了 NSFetchedResultsController 函数。然而,当拖动行时,我发现我必须将 fetchedResultsController 的委托(delegate)设置为 nil。

这是处理移动的代码部分。

func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {

// Create array of fetched items from the stack
var items = fetchedResultsController.fetchedObjects! as! [Item]

// Convert the index paths to row number in the table.
let destinationRow = rowAtIndexPath(destinationIndexPath)
var sourceRow = rowAtIndexPath(sourceIndexPath)

// Source row has to be reduced by one if it was moved to lower section because tableview is counting the row twice, once in the new section and once as the row index in the old section.
if sourceIndexPath.section > destinationIndexPath.section {
sourceRow -= 1
}

let newCategory = fetchedResultsController.sections![destinationIndexPath.section].name

// Get the item of from the source index path and update the category
let item = fetchedResultsController.objectAtIndexPath(sourceIndexPath) as! Item
item.category = newCategory

// Do not trigger delegate methods when changes are made to core data
fetchedResultsController.delegate = nil

// Move the item in core data
items.removeAtIndex(sourceRow)
items.insert(item, atIndex: destinationRow)


do {
try coreDataStack.context.save()
} catch {
fatalCoreDataError(error)
}

fetchedResultsController.delegate = self
}

我还没有找到任何可以回答这个问题的问题,尤其是在 Swift 中。非常感谢任何解决此问题的帮助。

最佳答案

为什么需要将delegate设置为nil?如果这是由于错误导致的,则表明您实现了错误的操作。

回答您原来的问题:由于您使用的是 NSFetchedResultsController,因此该部分将由 NSFetchedResultsControllerDelegate 方法 controller(_, didChangeSection, atIndex, forChangeType)

真正的问题是,为什么将delegate设置为nil

关于swift - 从节中移动最后一行时如何删除节标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36852646/

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