如何从MasterView的TableViewCell(Xib Cell)导航到DetailView的TableView

时间:2017-08-07 21:51:07

标签: ios swift xcode uitableview swift3

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

Master and Detail Views

现在我的问题是当我点击Master的TableViewCell时,我无法填充DetailView的表视图。我在下面粘贴我的代码:

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

}

1 个答案:

答案 0 :(得分:0)

我在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() }