从列表中选择并重新加载Tableview Swift [英] Selections from List and reload Tableview Swift

查看:104
本文介绍了从列表中选择并重新加载Tableview Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此很陌生.我在选择项目时遇到了问题,并在特定的索引单元(重新加载)上发回了tableview. 场景:我有两个视图控制器,并且都包含tableview.

I am quite new to this. I am getting an issue with selection items and post back to tableview on specific index cell (reload). Scenario: I have two view controllers and both contains tableview.

FirstViewController:我已经将来自Web服务的数据显示到Tableview中,但是在不同的操作类型上,我使用了didSelectRow函数,该函数向ListViewController显示以进行项目选择.在FirstViewController中,主要模型是Unit.

FirstViewController: I am already displaying data from webservice into Tableview but on different action types I have used didSelectRow function which shows to ListViewController for item selection. In FirstViewController main model is Unit.

ListViewController:显示在tableView中供选择的简单数据列表.

ListViewController: Simple list of data displaying in tableView for selection.

问题:是如何从ListViewController中选择项目并将数据传递给在特定单元格上发生更改的FirstViewController表格视图,然后我必须发布具有更新值的值.如何重新加载表格视图或模型?

Issue: is how can I select item from ListViewController and pass data to FirstViewController tableview that change occurs on specific cell and then I have to post values with updated values. How can I reload tableview or model?

型号:

struct Unit : Codable {

    let sectionList : [SectionList]?

}

struct SectionList : Codable {

    let title : String?
    let items : [Item]?
}


struct Item : Codable {

    let actionType : Int?
    let textField : String?
    let textValue : String?
    let pickList: [SectionList]?
    let multiSelect: Bool?
    let selectedValue: [String]?
    let version: Int?
    let masterId: Int?
    let itemValue: String?
}

更新的FirstController:

Updated FirstController:

class FirstViewController: UIViewControlller, ListDelegate {

   var selectedIndex: Int?
   var selectedSection: Int?

   //Click event for navigation from FirstViewController to SecondViewController
   @IBAction func BackButtonAction(_ sender: Any) {
       let vc = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
       vc.delegate = self
       self.navigationController?.pushViewController(vc, animated: true)
   }

   func listFunc(listValue: String) {
       AppData?.sectionList?[selectedSection!].items?[selectedIndex!].textField = listValue
       tableView.reloadData()
   }

   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       //Add these two line in your code
       selectedIndex = indexPath.row
       selectedSection = indexPath.section
   } 
}

已更新:ListViewController:

Updated : ListViewController:

protocol ListDelegate {
    func listFunc(listValue: String)
}

class SecondViewController: UIViewControlller {
    var delegate: ListDelegate?

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let indexPath = tableView.indexPathForSelectedRow
        let currentCell = tableView.cellForRow(at: indexPath!)!
        //print(currentCell.textLabel?.text as Any)
        currentCell.accessoryType = .checkmark
        delegate?.listFunc(listValue: currentCell.textLabel?.text ?? "")
        self.navigationController?.popViewController(animated: true)
   }
}

推荐答案

根据我的理解,我几乎无法理解您的代码.

I hardly able to understand your code, it's totally messed up according to me.

我发现您的大多数代码都已完成,我正在添加您需要做的其余事情.

I found that most of your code is done and I am adding rest your things that you need to do.

  1. 在您的 FirstViewController.swift didSelectRowAt函数中

let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
vc.Indexdata = // your data that you wanted to show in your next controller, 
// if variable is different then assign data to that var
vc.delegate = self
self.navigationController?.pushViewController(vc, animated: true)

  • ViewController.swift didSelectRowAt函数中

  • In ViewController.swift, didSelectRowAt func,

    // Comment these below lines
    
    // let vc = self.storyboard?.instantiateViewController(withIdentifier: "FirstViewController") as! FirstViewController
    // self.navigationController?.pushViewController(vc, animated: true)
    
    // Add below line        
    self.navigationController?.popViewController(animated: true)
    

  • 现在检查它是否可以正常工作.

    Now check it should work.

    这篇关于从列表中选择并重新加载Tableview Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆