Ios 删除TableViewCell,然后执行更多操作

Ios 删除TableViewCell,然后执行更多操作,ios,uitableview,swift,delegates,Ios,Uitableview,Swift,Delegates,我正在用Swift编码。我想通过从右向左滑动来删除TableViewCell,但我希望它快速、即时。 目前,我执行的工作不仅仅是从TableView中删除元素。这是我的密码: // Override to support editing the table view. override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRo

我正在用Swift编码。我想通过从右向左滑动来删除
TableViewCell
,但我希望它快速、即时。 目前,我执行的工作不仅仅是从
TableView
中删除元素。这是我的密码:

// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {

        // Delete the row from the data source 
        myRecomBottlesArray[0].removeFromRecomm(myRecomBottlesArray[indexPath.row])
        myRecomBottlesArray.removeAtIndex(indexPath.row)

        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

    } else if editingStyle == .Insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}
如您所见,我使用
myrecomballersarray.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath],带有rowAnimation:.Fade)
这两种方法速度非常快

我的问题是,我也在执行
myrecombletarray[0].removeFromRecomm(myrecombletarray[indexath.row])
,它从内存中加载一个表,从该表中删除元素,然后将该表再次保存到内存中。 因此,当我按下我的
Delete
按钮时,从我按下按钮到它实际删除GUI中的行之间有2秒的延迟

我想知道如何首先删除GUI中的行,然后在后台加载我的列表,删除元素并再次保存列表。通过这种方式,用户感觉它是即时的,当它在后台实际完成时,可以继续在应用程序上进行操作

我想我应该使用委托,但我在这方面很新,我不知道怎么做。如果需要,我可以提供更多的代码

以下是
removefromrecom()
函数的代码:

var recomBottlesArray = NSMutableArray()    

func removeFromRecomm(bottle: Bottle) {
    let bottleLoaded = Bottle.loadSaved()
    bottleLoaded?.recomBottlesArray.removeObject(bottle)
    bottleLoaded?.save()
}

class func loadSaved() -> Bottle? {
    if let data = NSUserDefaults.standardUserDefaults().objectForKey("bottleList") as? NSData {
        return NSKeyedUnarchiver.unarchiveObjectWithData(data) as? Bottle
    }
    return nil
}

func save() {
    let data = NSKeyedArchiver.archivedDataWithRootObject(self)
    NSUserDefaults.standardUserDefaults().setObject(data, forKey: "bottleList")
}

方法
removefromrecom
需要很长时间才能完成并阻塞主线程,这就是延迟的原因

您可以使用Grand Central Dispatch在后台线程中执行此方法:

dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), {
    myRecomBottlesArray[0].removeFromRecomm(myRecomBottlesArray[indexPath.row])
}
myRecomBottlesArray.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

还有其他在后台执行代码的方法,如
NSOperation
。你可以在报纸上读到他们。请注意UIKit不是线程安全的:对UI对象的所有操作都必须在主线程中完成。由于您的方法
removeFromRecomm
仅在模型上运行,因此在后台线程中调用它是安全的,

方法
removeFromRecomm
是否在UI上执行任何操作?或者它只更新模型?它不在UI上执行任何操作。它只涉及内存(
NSKeyedArchiver
更准确地说)。谢谢!为我工作:)