简体   繁体   English

从列表中选择并重新加载 Tableview Swift

[英]Selections from List and reload Tableview Swift

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: 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. FirstViewController:我已经将来自 webservice 的数据显示到 Tableview 中,但在不同的操作类型上,我使用了 didSelectRow 函数,该函数向ListViewController显示项目选择。 In FirstViewController main model is Unit.在 FirstViewController 中,主要模型是 Unit。

ListViewController: Simple list of data displaying in tableView for selection. ListViewController:简单的数据列表显示在tableView中供选择。

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.问题:我如何从 ListViewController 中选择项目并将数据传递给特定单元格上发生更改的 FirstViewController tableview,然后我必须发布具有更新值的值。 How can I reload tableview or model?如何重新加载表格视图或模型?

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?
}

Updated FirstController:更新的 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
   } 
}

Updated: ListViewController:更新: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)
   }
}

You've got a handful of options to accomplish this.您有几个选项可以完成此操作。 Assuming that the selected value is the only value that you've got to pass, using closures would be the easiest thing you could do.假设所选值是您必须传递的唯一值,那么使用闭包将是您可以做的最简单的事情。

1) Declare a closure that holds a type of your need inside ListViewController : 1)在ListViewController中声明一个包含您需要的类型的闭包:

`var didSelectItem : ((YourType, IndexPath) -> ())?`

2) In ListViewController 's didSelectRow method, use the closure: 2) 在ListViewControllerdidSelectRow方法中,使用闭包:

`self.didSelectItem?(yourDataSourceItemAtIndexpath, indexPath)`

3) You'll get a callback for the triggered closure when you use this closure from your ListViewController instance inside FirstViewController like: 3)当您从FirstViewController中的ListViewController实例使用此闭包时,您将获得触发闭包的回调,例如:

let myListInstance = ListViewController()

myListInstance.didSelectItem = { selectedItem, selectedIndex in
  //You'll get the selected item inside this block, do model updation here

}

After updating your model with new values, if you want to reload your tableView only at the particular(updated) index, you can use:使用新值更新模型后,如果您只想在特定(更新的)索引处重新加载 tableView,您可以使用:

self.yourTableView.reloadSections(sections: IndexSet, with: UITableView.RowAnimation) to reload a whole section or.. self.yourTableView.reloadSections(sections: IndexSet, with: UITableView.RowAnimation)重新加载整个部分或..

self.yourTableView.reloadRows(at: [IndexPath], with: UITableView.RowAnimation) to reload a row in a section self.yourTableView.reloadRows(at: [IndexPath], with: UITableView.RowAnimation)在一个部分重新加载一行

(If you're performing multiple updates simultaneously, keep in mind that you'll have to do these reloads inside self.yourTableView.beginUpdates() and self.yourTableView.endUpdates() ) (如果您同时执行多个更新,请记住您必须在self.yourTableView.beginUpdates()self.yourTableView.endUpdates()中进行这些重新加载)

NOTE:笔记:

You'll have to handle your dataSource properly during these reloads to make sure you have the right number of rows or sections before and after the reload, adding/subtracting the rows/sections that you insert/delete.在这些重新加载期间,您必须正确处理数据源,以确保在重新加载之前和之后拥有正确数量的行或部分,添加/减去您插入/删除的行/部分。 Else, your app will crash!否则,您的应用程序将崩溃!

If you don't want to risk that, just reload the whole table with yourTableView.reloadData()如果您不想冒险,只需使用yourTableView.reloadData()重新加载整个表

Also keep in mind that there are other ways to pass data across controllers and this is just a suggestion based on what I assume your use case to be还要记住,还有其他方法可以跨控制器传递数据,这只是基于我假设您的用例的建议

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. In your FirstViewController.swift , didSelectRowAt func,在你的FirstViewController.swift中, didSelectRowAt func,

     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)
  2. In ViewController.swift , didSelectRowAt func,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.现在检查它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM