标签显示数组中不可见的项目

时间:2016-08-29 08:50:04

标签: ios swift uitableview

现在我正在按照教程进行操作。我注意到当我点击一个表格单元格并重定向到一个新视图时,我发现每个单元格都没有细节。

ViewController.swift:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    var itemsArray: [ToDoItem] = []

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

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

        let tempItem = itemsArray[indexPath.row]

        cell.textLabel?.text = tempItem.itemName

        return cell!
    }


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "detailsegue" {
            let destVC = segue.destinationViewController as! DetailViewController

            let selectedIndex = tableView.indexPathForSelectedRow

            destVC.myItem = itemsArray[selectedIndex!.row] as? ToDoItem
        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        let item1 = ToDoItem(name: "Eat", desc: "Eat until i am full. I prefer local food though!", place: "Food Court")
        itemsArray.append(item1)

        let item2 = ToDoItem(name: "Drink", desc: "Drink until i am full. I prefer local drink though!", place: "Drink Court")
        itemsArray.append(item2)

        let item3 = ToDoItem(name: "Jump", desc: "Jump until i am tired. I prefer local jump though!", place: "Sport Center")
        itemsArray.append(item3)
        // 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.
    }
}

ToDoItem.swift:

import UIKit

class ToDoItem: NSObject {
    var itemName: String
    var itemDescription: String?
    var itemPlace : String?
    var completed: Bool

    init(name: String, desc: String?, place:String?) {
        self.itemName = name
        self.itemDescription = desc
        self.itemPlace = place
        self.completed = false
    }
}

my detailviewcontroller.swift

import UIKit

class DetailViewController: UIViewController {

    var myItem: ToDoItem?


    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var placeLabel: UILabel!
    @IBOutlet weak var descLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        nameLabel.text = myItem?.itemName
        placeLabel.text = myItem?.itemDescription
        // Do any additional setup after loading the view.
    }

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


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

这是我的故事板: storyboard

0 个答案:

没有答案