textLabel?.text 仅显示 "Label" 但不显示从前一个 Table 视图传递的实际数据

textLabel?.text displays only "Label" but not the actual data passed from previous Table View

我想要实现的目标:我想单击第一个视图控制器中的其中一行以移动到另一个 table 视图控制器。

问题:当我单击该行时,视图仅显示 "Label" 而不是我打算传递的数据。如果应用程序没有崩溃并且显示带有 "Label" 标题的白色背景,数据仍然没有显示在第二个视图控制器上。

到目前为止我所做的:我使用了结构:PageTwoItems 来定义我想要发送到第二个视图控制器的数据。

import Foundation
import UIKit

第二个视图控制器的代码如下:

class PageTwoTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!

var PageTwo = [String]()

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

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

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

   Cell.textLabel?.text = PageTwo[indexPath.row]

    return Cell
}

}

第一个视图控制器的代码如下:

@IBOutlet weak var tableView: UITableView!

let names = ["Cleaning", "Plumbing","Electrical", "Craftswork", "Automotive"]

let desc = ["Get your spotless home or office space", "Drains, Pipes, Faucets and more", "Lighting, Fans, AC's and more", "Installation, Assembly and more", "Tow Truck Hire, Tyre Replacement and more"]
let images = [UIImage(named:"pug"),UIImage(named:"pug2"),UIImage(named:"pug3"),UIImage(named:"pug4"),UIImage(named:"pug5")]

var PageTwo = [PageTwoItems]()


override func viewDidLoad() {

    PageTwo = [PageTwoItems(nametwo:["Home Cleaning", "Office Cleaning", "Moving In/Out Cleaning"], summarytwo:["Let your home sparkle","Office space cleaning right at your fingertips","New Home or Old Home? We've got you covered"],phototwo:["","",""]),
        PageTwoItems(nametwo:["Drains, Pipes & Faucets", "Showers and Bath Tubs", "Toilet and Wash Basin", "Water Heater"], summarytwo:["Fix Your Broken Pipes, Clogged Drains and Leaky Faucets","Showers and Bath Tubs working just right"," ", " "],phototwo:["","",""]),
        PageTwoItems(nametwo:["Lighting Fixtures", "Air Conditioners & Fans", "Generators"], summarytwo:["..","..",".."],phototwo:["","",""]),
        PageTwoItems(nametwo:["Furniture Assembly/Installation", "Interior Painting", "Doors, Windows & Curtains"], summarytwo:["..","...","..."],phototwo:["","",""]),
        PageTwoItems(nametwo:["Tow Truck Hire", "Tyre/Vulcanizer Help", "Auto-Consultant"], summarytwo:["...","...","..."],phototwo:["","",""])]



    super.viewDidLoad()

}

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 5

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.tableView.dequeueReusableCellWithIdentifier("newcell", forIndexPath:indexPath) as!CustomCell

    cell.photo.image = images[indexPath.row]
    cell.summary.text = desc[indexPath.row]
    cell.name.text = names[indexPath.row]

    return cell
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!

    let DestViewController = segue.destinationViewController as! PageTwoTableViewController

    var PageTwoArrayTwo : PageTwoItems

    PageTwoArrayTwo = PageTwo[indexPath.row]

    DestViewController.PageTwo = PageTwoArrayTwo.nametwo
    DestViewController.PageTwo = PageTwoArrayTwo.summarytwo
    DestViewController.PageTwo = PageTwoArrayTwo.phototwo

}

}

The struct for the Page Two Items: 

struct PageTwoItems {
    var nametwo : [String]
    var summarytwo : [String]
    var phototwo : [String]
}

我认为您使用的 segue 直接连接到您的第二个视图控制器的 tableview 单元格。

你应该在你的 2 个控制器之间连接你的 segue 并为属性检查器设置一个标识符,比如 showSecondControllerSegue 并使用这个方法:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier(identifier: "showSecondControllerSegue", sender: self)
}

编辑:-

您的代码应该是:

对于第二个视图控制器:

class PageTwoTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

var pageTwo = [String]()

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

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

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

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

   Cell.textLabel?.text = pageTwo[indexPath.row]

    return Cell
}
}

对于第一个视图控制器:

@IBOutlet weak var tableView: UITableView!

let names = ["Cleaning", "Plumbing","Electrical", "Craftswork", "Automotive"]

let desc = ["Get your spotless home or office space", "Drains, Pipes, Faucets and more", "Lighting, Fans, AC's and more", "Installation, Assembly and more", "Tow Truck Hire, Tyre Replacement and more"]
let images = [UIImage(named:"pug"),UIImage(named:"pug2"),UIImage(named:"pug3"),UIImage(named:"pug4"),UIImage(named:"pug5")]

var pageTwo = [PageTwoItems]()


override func viewDidLoad() {
    super.viewDidLoad()

    pageTwo = [PageTwoItems(nametwo:["Home Cleaning", "Office Cleaning", "Moving In/Out Cleaning"], summarytwo:["Let your home sparkle","Office space cleaning right at your fingertips","New Home or Old Home? We've got you covered"],phototwo:["","",""]),
        PageTwoItems(nametwo:["Drains, Pipes & Faucets", "Showers and Bath Tubs", "Toilet and Wash Basin", "Water Heater"], summarytwo:["Fix Your Broken Pipes, Clogged Drains and Leaky Faucets","Showers and Bath Tubs working just right"," ", " "],phototwo:["","",""]),
        PageTwoItems(nametwo:["Lighting Fixtures", "Air Conditioners & Fans", "Generators"], summarytwo:["..","..",".."],phototwo:["","",""]),
        PageTwoItems(nametwo:["Furniture Assembly/Installation", "Interior Painting", "Doors, Windows & Curtains"], summarytwo:["..","...","..."],phototwo:["","",""]),
        PageTwoItems(nametwo:["Tow Truck Hire", "Tyre/Vulcanizer Help", "Auto-Consultant"], summarytwo:["...","...","..."],phototwo:["","",""])]    
}

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.tableView.dequeueReusableCellWithIdentifier("newcell", forIndexPath:indexPath) as!CustomCell

    cell.photo.image = images[indexPath.row]
    cell.summary.text = desc[indexPath.row]
    cell.name.text = names[indexPath.row]

    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier(identifier: "showSecondControllerSegue", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!

    let DestViewController = segue.destinationViewController as! PageTwoTableViewController

    var pageTwoArrayTwo : PageTwoItems

    pageTwoArrayTwo = PageTwo[indexPath.row]

    DestViewController.pageTwo = pageTwoArrayTwo.nametwo
    DestViewController.pageTwo = pageTwoArrayTwo.summarytwo
    DestViewController.pageTwo = pageTwoArrayTwo.phototwo

}

我认为您的数据组织不正确。我认为您正在寻找的是这样的东西:

  1. 清洁

    • 家庭清洁
    • 办公室清洁
    • 等等
  2. 水暖

    • 下水道、管道和水龙头
    • 淋浴和浴缸
    • 等等
  3. 电气

    • 照明设备
    • 空调和风扇
    • 发电机

这就是我创建数据结构来支持它的方式。

// This represents a single row on the detail screen
struct PageTwoItem {
    var name : String
    var summary : String
    var photo : String
}
// this represents a single row on the main screen
struct PageData {
    var name: String            // goes into the table on main screen
    var subitems: [PageTwoItem] // send this to the detail
}

class MasterViewController: UITableViewController {

    var detailViewController: DetailViewController? = nil
    // Make an array of PageData
    var objects = [PageData]()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        setupData()
    }


    func setupData() {
        objects = [
            PageData(name: "Cleaning", subitems: [
                PageTwoItem(name: "Cleaning 1", summary: "", photo: ""),
                PageTwoItem(name: "Cleaning 2", summary: "", photo: ""),
                PageTwoItem(name: "Cleaning 3", summary: "", photo: ""),
                PageTwoItem(name: "Cleaning 4", summary: "", photo: "")
                ] ),
            PageData(name: "Plumbing", subitems: [] ),
            PageData(name: "Electrical", subitems: [] ),
            PageData(name: "Craftswork", subitems: [] ),
            PageData(name: "Automotive", subitems: [] ),
        ]
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        print(segue.identifier)
        if segue.identifier == "showDetail" {
            if let indexPath = self.tableView.indexPathForSelectedRow {
                let object = objects[indexPath.row]
                let controller = segue.destinationViewController as! DetailViewController
                // tell the detail controller what we want her to show
                controller.detailItem = object
            }
        }
    }

    // MARK: - Table View

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

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

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

        let object = objects[indexPath.row]
        cell.textLabel!.text = object.name
        return cell
    }


}

在详细控制器中:-

导入 UIKit

class DetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!
    // here is where we put the data we want to show
    var detailItem: PageData?

    // MARK: - Table View

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

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let detailItem = detailItem {
            print("count = \(detailItem.subitems.count)")
            return detailItem.subitems.count
        }
        return 0

    }

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

        if let object = detailItem?.subitems[indexPath.row] {
            cell.textLabel!.text = object.name
        }
        return cell
    }
}