首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Swift中删除UITableView中的单元格

在Swift中删除UITableView中的单元格
EN

Stack Overflow用户
提问于 2015-12-22 03:58:43
回答 2查看 595关注 0票数 1

从tableView中删除单元格时,动画运行良好,但tableView数据不会重新加载。我试着在没有.beginUpdates().endUpdates()的情况下运行,而是使用.reloadData(),这个过程可以工作,但是动画不能工作。

代码语言:javascript
运行
复制
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    switch editingStyle {
    case .Delete:
        // remove the deleted item from the model
        let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let context:NSManagedObjectContext = appDel.managedObjectContext
        context.deleteObject(coreData[indexPath.row] as NSManagedObject)
        coreData.removeAtIndex(indexPath.row)
        do {
            try context.save()
        } catch {
            print("Error deleting NSManagedObject")
        }


        tableView.beginUpdates()
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        tasks.removeAtIndex(indexPath.row)
        names.removeAtIndex(indexPath.row)
        ids.removeAtIndex(indexPath.row)
        tableView.endUpdates()
        // remove the deleted item from the `UITableView`

    default:
        return

    }

}

顺便说一下,tasks数组是tableView的主要数据。

如何在删除单元格时重新加载数据,并使用平滑的删除动画?

EN

Stack Overflow用户

回答已采纳

发布于 2015-12-22 04:20:27

有两件事需要考虑:

  1. 更新表视图之前,必须更新数据模型。因此,守则的顺序必须是: tasks.removeAtIndex(indexPath.row) names.removeAtIndex(indexPath.row) ids.removeAtIndex(indexPath.row) tableView.deleteRowsAtIndexPaths(indexPath,withRowAnimation:.Fade)
  2. 不需要调用begin/endUpdates,因为您只进行一次调用来更新表视图。
票数 4
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34408215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档