SWIFT中风投之间的短期记忆

人气:171 发布:2022-10-16 标签: ios swift segue tableviewcell

问题描述

简称:

一个VC的变量值在另一个VC的模式呈现和解聘期间保持不变吗?当第二个VC被取消时,原始VC的变量是否仍等于其上一个值?

详细信息(如果需要)

我有一个视图控制器,其中选择了一个表格单元格。然后,该单元格的内容被提取出来并传递到一个编辑器视图控制器中,如下所示:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    //Segue to mainVC editor, for editing action
    if (segue.identifier == "modalToEditor") && passingEdit == true {
        //Assign selection to a variable 'currentCell'
        let indexPath = tableView.indexPathForSelectedRow;
        let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! CustomTableViewCell;

        //Set cell text into variables to pass to editor
        let cellNameForEdit = currentCell.nameLabel!.text
        let cellDescForEdit = currentCell.descLabel.text

        //Pass values to EditorView
        let editorVC = segue.destinationViewController as! EditorView;
        editorVC.namePassed = cellNameForEdit
        editorVC.descPassed = cellDescForEdit
        editorVC.indexOfTap = indexPath
        editorVC.currentListEntity = currentListEntity
现在,在第二个/EDITOR视图控制器中,用户可以点击一个按钮,请求移动单元格。"Move Screen"是一个不同的/第三个VC。我想知道的是,我是否可以取消编辑,并期望原始VC记住最后选择的单元格?

如果它会记住,那么我假设我可以将它传递给第三个/Move VC。 如果原来的VC单元格变量不再保存最后一个单元格,我将不得不想办法让它记住!可能是全局变量?

追加编辑以显示cell ForRowAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    //Setup variables
    let cellIdentifier = "BasicCell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CustomTableViewCell

    //Make sure the row heights adjust properly
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 80.0


    //Create normal cells except when last cell
    if indexPath.row < taskList_Cntxt.count {
        let task =  taskList_Cntxt[indexPath.row]
        //Create table cell with values from Core Data attribute lists
        cell.nameLabel!.text = task.valueForKey("name") as? String
        cell.descLabel!.text = task.valueForKey("desc") as? String

        //Related to running TaskActions: Empty block function passed from custom cell VC
        cell.doWork = {
            () -> Void in
            self.doStuff(cell)
        }
    }

推荐答案

是。视图控制器只是对象,因为只有在执行更改它们的代码时才会更改它们的属性。在您的特定情况下,VC将其表视图保留为属性(并且强烈地将其作为其视图的子视图),而表视图保留所选索引路径的数组。不过要小心,UITableViewController的子类可以在默认情况下清除view WillPresar(see here)上的选择。

还请注意,您选择了一种大多数人会认为奇怪的方法来初始化prepareForSegue中的editorVC。获取选定的索引路径很好,但是获取一个单元格(一个视图),然后配置该单元格,然后从单元格的子视图中获取数据源值,这是非常复杂的。

看到您的cellForRowAtIndexPath方法中的let task = taskList_Cntxt[indexPath.row]行吗?这就是如何在给定的indexPath处访问数据源数组中的对象。该对象(您已经称为task)应该传递给下游视图控制器。

600