【问题标题】:How Can I show data to tableView From Array which belong in model array object in UITableView?如何从属于 UITableView 中的模型数组对象的数组中向 tableView 显示数据?
【发布时间】:2018-08-23 08:32:55
【问题描述】:

我有一个模型对象,它有一个数组,我通过 indexPath.row 填充数据,而不是我需要将每个部分数组发送到另一个 tableView,但它向我发送随机数据我如何将特定选定的行数组发送到下一个 tableView?请帮助我理解..

这是我的模型对象:

class ProductModel {

    var name:String
    var option: [String]
    var image:UIImage
    init(name:String,option:[String],image:UIImage) {
        self.image = image
        self.name = name
        self.option = option
    }
}

这是我的模型对象:

class Data {

    static var product = [ProductModel]()

}

这里是追加数据的数据函数

static func read(compleationHandler:@escaping () -> ()) {

    let hospitalList:[String] =  ["A","B,C"]
    let hospitalTwo:[String] = ["Sharee bangla","National Park","bangladesh medical"]
    let hospitalThree:[String] = ["NSU","MIU","UIU","AIUB"]
    let hospitalFour:[String] = ["Dhaka","Comillah","Borials"]
    let hospitalFive:[String] = ["iPhone","iPad","iMac"]

    DispatchQueue.global(qos: .userInteractive).async {


        if Data.product.count == 0 {

            Data.product.append(ProductModel(name: "Mahmud", option: hospitalList, image: #imageLiteral(resourceName: "radio")))
            Data.product.append(ProductModel(name: "Rangon", option: hospitalTwo, image: #imageLiteral(resourceName: "pause.on.fw")))
            Data.product.append(ProductModel(name: "Saikot", option: hospitalThree, image: #imageLiteral(resourceName: "Unit.fw")))
            Data.product.append(ProductModel(name: "Ratul", option: hospitalFour, image: #imageLiteral(resourceName: "Region.fw")))
            Data.product.append(ProductModel(name: "Jubayer", option: hospitalFive, image: #imageLiteral(resourceName: "add-note.fw")))

        }
        DispatchQueue.main.async {
            compleationHandler()
        }
    }
}

这里是第一个 ViewDidLoad 和一个用于存储单元数组的全局数组:

class ModelTableView: UIViewController {
    var optionData:[String] = [String]()


    @IBOutlet var ModelDataTableVIew: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
         ModelDataTableVIew.delegate = self
        ModelDataTableVIew.dataSource = self
        ModelFunction.read { [weak self] in
            self?.ModelDataTableVIew.reloadData()
        }
    }

}

这是我在标签中创建的 cellForRowAtIndex,我添加了一个点击手势。当用户单击它时,将显示另一个带有此元胞数组数据的 tableView

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        if indexPath.row  == Data.product.count {
            let cell = ModelDataTableVIew.dequeueReusableCell(withIdentifier: "modelCellBtn") as! ModelCellBtn
            return cell
        } else {
            let cell = ModelDataTableVIew.dequeueReusableCell(withIdentifier: "modelCell") as! ModelCell
            cell.configureCell()
            optionData = Data.product[indexPath.section].option
            let tapGeshture = UITapGestureRecognizer(target: self, action: #selector(tapped(_:)))
            cell.optionLabel.addGestureRecognizer(tapGeshture)
            cell.nameImage.image = Data.product[indexPath.row].image

            return cell
        }

这是准备 Segue 并将数据发送到下一个视图控制器:

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if (segue.identifier == "option") {
            let dest = segue.destination as! OptionViewController
            dest.data = optionData

        }

    }

这是我的 nextViewController(用于显示选项数据):

class OptionViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

        var data:[String] = []

        @IBOutlet weak var optionTableView: UITableView!
        override func viewDidLoad() {
            super.viewDidLoad()

            optionTableView.delegate = self
            optionTableView.dataSource = self
            print(data)
            optionTableView.reloadData()
            ModelFunction.read { [weak self] in
                self?.optionTableView.reloadData()
            }

        }
    }

这里选项 View Controller 和在这个控制器中我想在 tableView 中显示检索到的数组

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = optionTableView.dequeueReusableCell(withIdentifier: "optionCell") as! OptionCell
        //cell.textLabel?.text = Data.product[indexPath.section].option[indexPath.row]
        cell.textLabel?.text = data[indexPath.row]
    return cell
    }

但它在我的 tableView 中向我显示了单节数据..

【问题讨论】:

  • 首先从不将自定义类命名为Data。它可能会干扰内部结构Data。并且变量名应该总是以小写字母开头。在表格视图中,委托方法始终使用传递的 tableView 实例,而不是硬编码的属性。

标签: ios swift uitableview


【解决方案1】:

您在 cellForRow 方法中采用 optionData,该方法始终采用 lastVisible 数据,您应该在 didSelectRowAt 方法中采用 optionData

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("didselect")
    optionData = Data.product[indexPath.row].option
    performSegue(withIdentifier: "option", sender: self)
}

尝试并分享您的结果。

【讨论】:

  • 它将第一节数据发送到下一个 viewController (tableView) / A B,C
  • 您应该在 cellForRow 中注释手势部分。这两条线。让 tapGeshture = UITapGestureRecognizer(target: self, action: #selector(tapped(_:))) cell.optionLabel.addGestureRecognizer(tapGeshture).
  • 做了但结果相同我认为这两行不是发送数据的一部分
  • 是的,这不是发送数据的一部分,但我认为 tapped(_ :) 方法可能有问题。
  • tapped(_ :) 方法中有什么,您能否发布该代码以更好地理解问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多