从tableview到详细的讨论都没有用

时间:2016-06-04 20:53:16

标签: ios swift uitableview segue

我正在为我们镇上的商店建立一个桌面视图,我希望能够点击商店的名称来查看更多细节。您可以找到main.storyboard和文件here的一些图片。我还点击了这个问题的视频。

masterViewController的代码如下所示:

import UIKit

class MasterTableViewController: UITableViewController {

// MARK: - Properties
var detailViewController: DetailViewController? = nil
var winkels = [Winkel]()

// MARK: - View Setup
override func viewDidLoad() {
    super.viewDidLoad()

    winkels = [
        Winkel(category:"Literature", name:"Standaard"),
        Winkel(category:"Literature", name:"Acco"),
        Winkel(category:"Clothing", name:"H&M"),
        Winkel(category:"Clothing", name:"C&A"),
        Winkel(category:"Clothing", name:"Patio"),
        Winkel(category:"Restaurants", name:"De 46"),
        Winkel(category:"Restaurants", name:"Het hoekske"),
        Winkel(category:"Supermarkets", name:"Carrefour"),
        Winkel(category:"Supermarkets", name:"Colruyt")
    ]

    winkels.sortInPlace({ $0.name < $1.name })

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

override func viewWillAppear(animated: Bool) {
    clearsSelectionOnViewWillAppear = splitViewController!.collapsed
    super.viewWillAppear(animated)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

// MARK: - Table View
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

    let winkel = winkels[indexPath.row]
    cell.textLabel!.text = winkel.name
    cell.detailTextLabel?.text = winkel.category
    return cell
}

// MARK: - Segues
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetail" {
        if let indexPath = tableView.indexPathForSelectedRow {
            let winkel = winkels[indexPath.row]
            let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
            controller.detailWinkel = winkel
            controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem()
            controller.navigationItem.leftItemsSupplementBackButton = true
        }
    }
 }

}

最后一部分显示了到下一个导航控制器的segue(名为showDetail,属于&#34;显示详细信息(例如替换)&#34;。

以下是detailViewController的代码:

import UIKit

class DetailViewController: UIViewController {

@IBOutlet weak var detailDescriptionLabel: UILabel!
@IBOutlet weak var WinkelImageView: UIImageView!

var detailWinkel: Winkel? {
    didSet {
        configureView()
    }
}

func configureView() {
    if let detailWinkel = detailWinkel {
        if let detailDescriptionLabel = detailDescriptionLabel, WinkelImageView = WinkelImageView {
            detailDescriptionLabel.text = detailWinkel.name
            WinkelImageView.image = UIImage(named: detailWinkel.name)
            title = detailWinkel.category
        }
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    configureView()
}

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

}

我不确定我做错了什么,以便点击无法正常工作..先谢谢你教我如何解决这个问题!

1 个答案:

答案 0 :(得分:1)

您需要在第一个类中为tableview实现方法didSelectRowAtIndexPath。在这里,您将执行实际从一个ViewController移动到另一个ViewController的segue。