我有一个主从视图应用程序,其中MasterView的表单元格是Xib单元格。我还在DetailViewController中有两个TableViews并排放置,如下所示:

Master and Detail Views

现在我的问题是,当我单击Master的TableViewCell时,我无法填充DetailView的Table View。我在下面粘贴我的代码:

class MasterViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var devarlViewController: DetailViewController? = nil
var objects = [Any]()
var statesList = ["AL", "GA", "AK", "AR", "AZ", "CA", "CO", "CT", "CO"]

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
    // Do any additional setup after loading the view, typically from a nib.
    }


// MARK: - Table View

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return statesList.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel!.text = statesList[indexPath.row]
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    DetailViewController.sharedDetail.selectedState = (self.tableView(tableView, cellForRowAt: indexPath).textLabel?.text)!
    DetailViewController.sharedDetail.selectedCity = ""
}


}

class DetailViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
static let sharedDetail = DetailViewController()
@IBOutlet weak var CitiesTable: UITableView!

@IBOutlet weak var PlacesTable: UITableView!
var selectedState: String = ""{
    didSet{
        CitiesTable.reloadData()
    }
}
var selectedCity: String = ""
var citiesStates: [String:[String]] = [ "AL": ["Auburn", "Montgomery", "Birmingham"]

]
var placesCities: [String:[String]] = [ "Auburn": ["Dominos", "Pizzahut", "McDonalds"],
                                        "Birmingham": ["iHop", "Coldstone", "WaffleHouse"]
]
var cities: [String] = []
var places: [String] = []
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

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

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if(tableView == CitiesTable)
    {
        if(selectedState != "")
        {
            cities = citiesStates[selectedState]!
            return cities.count
        }
    }
    else
    {
        if(selectedCity != "")
        {
            places = placesCities[selectedCity]!
            return places.count
        }
    }
    return 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if(tableView == CitiesTable)
    {
      let cell = tableView.dequeueReusableCell(withIdentifier: "citiesCell", for: indexPath)
        cell.textLabel?.text = cities[indexPath.row]
        return cell
    }
    else
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "placesCell", for: indexPath)
        cell.textLabel?.text = cities[indexPath.row]
        return cell
    }
}


}

最佳答案

我在如下所示的masterviewcontroller中添加了一个协议

protocol MasterSelectionDelegate: class {
   func itemSelected(_ item: AnyObject)
}


然后,因为splitviewcontroller作为rootviewcontroller而坐。
在应用程序委托中或在创建Split View Controller期间。我在下面添加了

masterViewController.delegate = detailViewController


在主ViewController的didSelectRowAtIndexPath中,我将其称为:

`override func tableView(tableView: UITableView,
 didSelectRowAtIndexPath indexPath: NSIndexPath) {
 let selectedItem = self.arrString[indexPath.row]
 self.delegate?.itemSelected(selectedItem)
}`


最后,您必须在详细视图控制器的委托中实现它

func itemSelected(_ item: AnyObject) { itemSelected = item as? Array<String> tableview.reloadData() }

10-08 11:33