更新uisplitviewcontroller的detailview中的表

时间:2017-05-02 16:39:46

标签: ios swift uitableview uisplitviewcontroller

所以我有一个UISplitViewController,在DetailView中我有一个UITableView。

我想要做的是从根视图控制器中获取所选单元格并将其附加到在详细视图控制器中填充我的tableview的字符串。我可以很容易地附加选择,但我需要帮助的是更新部分。

应该发生的是当您单击单元格时,它会将单元格中的值附加到全局变量main.BoatManifest,然后更新表格。我只是不知道该怎么做。

以下是一些代码:

import UIKit

class DetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, FullListCellDelegate {

@IBOutlet weak var tableView: UITableView!

func updateName(sender: FullListCell, detVC: DetailViewController) {
    tableView.reloadData()
}

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return main.BoatManifest.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

 // Configure the cell...
    cell.textLabel?.text = main.BoatManifest[indexPath.row]


 return cell
 }


}

struct Main {

var BoatManifest: [String] = []

}

var main = Main(BoatManifest: [" "])

这是我的详细信息视图的代码,底部的结构Main只是一个全局变量。我的MainView代码是这样的:

import UIKit

protocol NameSelectionDelegate: class {
func nameSelected()
}

class MasterViewController: UITableViewController {


var Names: [String] = ["John", "Mark", "Paul", "Jeremy"]

override func viewDidLoad() {
    super.viewDidLoad()

    if let split = self.splitViewController {
        let controllers = split.viewControllers
        self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
    }

    }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return Names.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

    cell.textLabel?.text = Names[indexPath.row]

    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    main.BoatManifest.append(Names[indexPath.row])
    NotificationCenter.default.addObserver(self, selector: #selector(loadList), name: NSNotification.Name(rawValue: "load"), object: nil)


}
func loadList(){
    //load data here
    self.tableView.reloadData()
}

}

我尝试使用UISplitViewController教程来解决它,但它没有详细视图控制器中的tableview。当我选择单元格到详细的tableview时,我需要发送一条消息,并使用附加的文本进行更新。

感谢您的帮助。

0 个答案:

没有答案