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

时间:2019-11-29 07:38:14

标签: ios swift uitableview model didselectrowatindexpath

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

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

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

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

型号:

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

2 个答案:

答案 0 :(得分:0)

您可以通过多种选择来完成此任务。假设所选值是唯一要传递的值,那么使用闭包将是您最容易做的事情。

1)在ListViewController中声明一个满足您需求的闭包:

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

2)在ListViewController的{​​{1}}方法中,使用闭包:

didSelectRow

3)当您在`self.didSelectItem?(yourDataSourceItemAtIndexpath, indexPath)` 内的ListViewController实例中使用闭包时,您将得到触发闭包的回调,例如:

FirstViewController

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

let myListInstance = ListViewController() myListInstance.didSelectItem = { selectedItem, selectedIndex in //You'll get the selected item inside this block, do model updation here } 重新加载整个部分或..

self.yourTableView.reloadSections(sections: IndexSet, with: UITableView.RowAnimation)重新加载节中的行

(如果您要同时执行多个更新,请记住,您必须在self.yourTableView.reloadRows(at: [IndexPath], with: UITableView.RowAnimation)self.yourTableView.beginUpdates()内部进行这些重新加载)

注意:

在这些重新加载期间,您必须正确处理dataSource,以确保在重新加载之前和之后具有正确数量的行或节,添加/减去插入/删除的行/节。否则,您的应用程序将崩溃!

如果您不想冒险,只需用self.yourTableView.endUpdates()

重新加载整个表

还请记住,还有其他方法可以跨控制器传递数据,这只是基于我假设您的用例的建议

答案 1 :(得分:0)

我几乎听不懂您的代码,根据我的理解,这完全是一团糟。

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


  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)
    
  2. ViewController.swift didSelectRowAt函数中,

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

现在检查它是否可以工作。

相关问题