簡體   English   中英

從列表中選擇並重新加載 Tableview Swift

[英]Selections from List and reload Tableview Swift

我對此很陌生。 我在選擇項目時遇到問題,並在特定索引單元格上發回表視圖(重新加載)。 場景:我有兩個視圖控制器,都包含表視圖。

FirstViewController:我已經將來自 webservice 的數據顯示到 Tableview 中,但在不同的操作類型上,我使用了 didSelectRow 函數,該函數向ListViewController顯示項目選擇。 在 FirstViewController 中,主要模型是 Unit。

ListViewController:簡單的數據列表顯示在tableView中供選擇。

問題:我如何從 ListViewController 中選擇項目並將數據傳遞給特定單元格上發生更改的 FirstViewController tableview,然后我必須發布具有更新值的值。 如何重新加載表格視圖或模型?

模型:

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:

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:

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

您有幾個選項可以完成此操作。 假設所選值是您必須傳遞的唯一值,那么使用閉包將是您可以做的最簡單的事情。

1)在ListViewController中聲明一個包含您需要的類型的閉包:

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

2) 在ListViewControllerdidSelectRow方法中,使用閉包:

`self.didSelectItem?(yourDataSourceItemAtIndexpath, indexPath)`

3)當您從FirstViewController中的ListViewController實例使用此閉包時,您將獲得觸發閉包的回調,例如:

let myListInstance = ListViewController()

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

}

使用新值更新模型后,如果您只想在特定(更新的)索引處重新加載 tableView,您可以使用:

self.yourTableView.reloadSections(sections: IndexSet, with: UITableView.RowAnimation)重新加載整個部分或..

self.yourTableView.reloadRows(at: [IndexPath], with: UITableView.RowAnimation)在一個部分重新加載一行

(如果您同時執行多個更新,請記住您必須在self.yourTableView.beginUpdates()self.yourTableView.endUpdates()中進行這些重新加載)

筆記:

在這些重新加載期間,您必須正確處理數據源,以確保在重新加載之前和之后擁有正確數量的行或部分,添加/減去您插入/刪除的行/部分。 否則,您的應用程序將崩潰!

如果您不想冒險,只需使用yourTableView.reloadData()重新加載整個表

還要記住,還有其他方法可以跨控制器傳遞數據,這只是基於我假設您的用例的建議

我幾乎無法理解您的代碼,根據我的說法,它完全搞砸了。

我發現您的大部分代碼都已完成,我正在添加您需要做的其他事情。


  1. 在你的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. 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)

現在檢查它應該工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM