代码之家  ›  专栏  ›  技术社区  ›  Will Zimmer

将tableView数据移动到另一个具有单独实体名称的tableView

  •  0
  • Will Zimmer  · 技术社区  · 10 年前

    我试图使用一个按钮一次移动我的tableView的整个部分。我希望它将所有选定的行移动到另一个tableView,并从第一个表中删除它们。第一个tableView数据保存在SList中,第二个是SCList。这两个实体都有属性item、qty、desc,仅第一个字母从sl变为sc(即slitem变为scitem)。我遇到的问题是将数据从SList实体更改为SCList实体。如何从SList实体中删除数据并更改为SCList并将其添加到相应的tableView?

    按钮代码:

    @IBAction func Move(sender: AnyObject) {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! SLTableViewCell
        let passValue = cell.cellLabel!.text
        print(passValue)
        self.performSegueWithIdentifier("moveToSC", sender: self)
    }
    

    分段代码:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if segue.identifier == "editItem" {
            let SListController:SLEdit = segue.destinationViewController as! SLEdit
            SListController.item = selectedItem
        }
        if segue.identifier == "moveToSC" {
            let SCListController:SCart = segue.destinationViewController as! SCart
            SCListController.item = item
        }
    

    我注释了“if-segue.identifier==”moveToSC“”代码,它会分段,但不会传递任何信息。所有打印内容都是“可选(“标签”)”,没有任何内容通过或删除。

    我不确定是否需要其他代码。如果你需要查看任何内容,请告诉我,我会编辑我的代码。

    我试图不使用didSelectRow方法,因为这会将行从第0节更改为第1节。我想使用一个按钮一次性移动所有行。我甚至必须与实体打交道吗?或者它应该自己移动和改变它吗?我对此很迷茫。。。

    1 回复  |  直到 10 年前
        1
  •  0
  •   pbasdf    10 年前

    您问题的直接答案是,您需要创建 SCList ,将其属性值设置为与 SList 对象,然后删除 SList列表 对象我从前面的问题中了解到,您正在为第一个tableView使用一个fetchedResultsController,包含两个部分;要移动的对象位于第1部分:

    @IBAction func Move(sender: AnyObject) {
        let sectionInfo = self.fetchedResultsController.sections![1] 
        let objectsToMove = sectionInfo.objects as! [SList]
        for item in objectsToMove {
            let newItem = NSEntityDescription.insertNewObjectForEntityForName("SCList", inManagedObjectContext:self.managedObjectContext!) as! SCList
            newItem.scitem = item.slitem
            newItem.scqty = item.slqty
            newItem.scdesc = item.scdesc
            self.managedObjectContext.deleteObject(item)            
        }
        self.performSegueWithIdentifier("moveToSC", sender: self)
    }
    

    对于您的问题,更长的答案是,像这样将数据从一个实体复制到另一个实体是不理想的——这表明您可能需要进一步思考您的数据模型。例如,您可以有一个附加属性,该属性反映项目应该出现在哪个tableView中。您将为获取的结果控制器指定谓词,以确保表视图仅显示适当的项。然后,“移动”一个项目只需更改新属性的值即可。