【问题标题】:How do I pass data after reloading the cell to another view controller将单元格重新加载到另一个视图控制器后如何传递数据
【发布时间】:2021-08-08 10:31:05
【问题描述】:

我有一个视图控制器,它对第一个视图控制器中的单元格重新排序,但重新排序未反映在第二个视图控制器中。我尝试使用结构传递数据,但这是不可能的。使用 commonInit 我将详细信息传递给第二个视图控制器

import UIKit
import CoreData

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return name.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return  95
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! PizzaListTableViewCell
      /*  let data = pizza[indexPath.row]
        
        cell.textLabel?.text = data.name
        cell.textLabel?.text = data.miscellaneousText
        cell.textLabel?.text = data.amount */

        cell.commonInit(_imageName: "pizza_\(indexPath.item)", title:name[indexPath.item], text: miscellaneousText[indexPath.item], amount: amount[indexPath.item])
        return cell
    }
    func  tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = DetailsVC()
        vc.commonInit("pizza_\(indexPath.item)", title:name[indexPath.item],text: miscellaneousText[indexPath.item], amount: amount[indexPath.item],pizzaTextField: pizzaToppings[indexPath.item])
        
        self.navigationController?.pushViewController(vc, animated: true)
        self.tableView.deselectRow(at: indexPath, animated: true)
    }
    func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        
        let item = name[sourceIndexPath.row]
        name.remove(at: sourceIndexPath.row)
        name.insert(item, at: destinationIndexPath.row)
        
    }
     

下一个视图控制器名称为 DetailsVC()。重新排序未反映在此视图控制器中。

import UIKit
import CoreData

class DetailsVC: UIViewController, UIPickerViewDataSource ,UIPickerViewDelegate{
    let name = ["Bacon Cheese Fry","Pizza Margherita"]
    let miscellaneousText = ["Accepted","Accepted"]
    let amountArr = ["39.99","39.99"]
     
    @IBOutlet weak var detailImage: UIImageView!
    var imageName: String!
    var titleName: String!
    
    @IBOutlet weak var text: UILabel!
    var textName: String!
    
    var textDescript: String!
    
    @IBOutlet weak var amount: UILabel!
    var amountName: String!
     func commonInit(_ imageName: String, title: String, text: String, amount: String,pizzaTextField:String)
    {
        self.imageName = imageName
        self.title = title
        self.textName = text
        self.amountName = amount
        self.pizzaText = pizzaTextField
       
        
    }

【问题讨论】:

  • DetailsVC() 不会返回您期望的实例。这是一个新实例,而不是故事板中的实例。您必须使用 segue 或实例化实例。并且永远不要使用多个数组作为数据源。这是非常糟糕的做法。

标签: ios swift uitableview xcode8 reorderlist


【解决方案1】:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let storyBoard = UIStoryboard(name: "StoryboardName", bundle: nil)
            let vc =  storyBoard.instantiateViewController(withIdentifier: "yourVCIdentifier") as? DetailsVC
            vc?.commonInit("pizza_\(indexPath.item)", title:name[indexPath.item],text: miscellaneousText[indexPath.item], amount: amount[indexPath.item],pizzaTextField: pizzaToppings[indexPath.item])
            self.navigationController?.pushViewController(vc, animated: true)
            self.tableView.deselectRow(at: indexPath, animated: true)
 }

编辑:

使用 XIB

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                let vc =  DetailVC(nibName: "YourNibName", bundle: nil)
                vc.commonInit("pizza_\(indexPath.item)", title:name[indexPath.item],text: miscellaneousText[indexPath.item], amount: amount[indexPath.item],pizzaTextField: pizzaToppings[indexPath.item])
                self.navigationController?.pushViewController(vc, animated: true)
                self.tableView.deselectRow(at: indexPath, animated: true)
}

【讨论】:

  • 我使用 xib 文件在视图控制器之间导航。我该如何实现呢?
  • *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法在捆绑包中加载 NIB:'NSBundle /admin/Library/Developer/CoreSimulator/Devic es/5B178677- 7C10-4F6F-BF5C-51E456C3BDAD/data/Containers/Bundle/Application/8CABC323-625B-45B8-878B-EDB5A75F49BF/PizzaCli.app>(已加载)',名称为'DetailsVC.xib'
  • 在我尝试运行代码时出现此错误
  • @ThrinathSirna 如果你的包名是“DetailsVC”,那么只使用“DetailsVC”而不使用“.xib”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多