在TableView和Swift

时间:2017-06-15 08:12:58

标签: ios swift tableview uistoryboardsegue

我正在尝试在tableView中显示事件列表,当用户按下其中一个事件时,它将打开另一个viewController,其中包含有关该特定选择的更多信息。

这是tableView,其中包含可以选择的选项:

import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase

class NewsfeedViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var ref:DatabaseReference!,
        posts = [eventStruct]()
    @IBOutlet weak var tableview: UITableView!
    var propertStruct : (Any)? = nil


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

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

    func loadNews() {
        ref = Database.database().reference()
        ref.child("events").queryOrderedByKey().observe(.childAdded, with: { (snapshot) in

            if let valueDictionary = snapshot.value as? [AnyHashable:String]
            {
                let title = valueDictionary["Title"]
                let location = valueDictionary["Location"]
                let date = valueDictionary["Date"]
                let description = valueDictionary["Description"]
                self.posts.insert(eventStruct(title: title, date: date, location: location, description: description), at: 0)
                self.tableview.reloadData()
            }
        })

    }

    /////////////////////////     Table View Content     \\\\\\\\\\\\\\\\\\\\\\\\\\
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        let label1 = cell.viewWithTag(1) as! UILabel
        label1.text = posts[indexPath.row].title
        let label2 = cell.viewWithTag(2) as! UILabel
        label2.text = posts[indexPath.row].location
        let label3 = cell.viewWithTag(3) as! UILabel
        label3.text = posts[indexPath.row].date
        return cell
    }

    func prepareForSegue(segue: UIStoryboardSegue, sender: eventStruct) {
        let secondViewController = segue.destination as? EventViewController
        secondViewController?.data = sender
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "showDetails", sender: posts[indexPath.row])
    }
    /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
}

struct eventStruct {
    let title: String!
    let date: String!
    let location: String!
    let description: String!


}

以下DetailsViewController应显示在上一个tableView中选择的其他选项的标题:

import UIKit

class EventViewController: UIViewController {

    var data: eventStruct?
    @IBOutlet weak var titleLabel: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()
        print (data?.title as Any)
        self.titleLabel.text = self.data?.title

        // 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 prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

问题是,不是显示应存储在struct变量中的标题,而是始终返回nil值。

有人能告诉我我做错了什么吗?我试图遵循所有的建议,但没有任何作用。

4 个答案:

答案 0 :(得分:2)

您的prepareForSegue必须覆盖

的UIViewController方法
func prepare(for segue: UIStoryboardSegue, sender: Any?)

因此NewsfeedViewController中的方法应为

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let secondViewController = segue.destination as? EventViewController
    secondViewController?.data = sender as? eventStruct
}

添加一些错误检查

也是一个好主意

答案 1 :(得分:1)

.vs中的属性从EventViewCotntroller更改为AnyData,如:

eventStruct

然后在表格控制器中:

var data: eventStruct?

请记住,func prepareForSegue(segue: UIStoryboardSegue, sender: eventStruct?) { let secondViewController = segue.destination as? EventViewController secondViewController?.data = sender } 必须超出您的TableViewControlle类范围:

eventStruct

答案 2 :(得分:1)

尝试以下

在EventViewController中

var propertStruct : (Any)? = nil

传递数据时

secondViewController.propertStruct =   eventStruct(title : "", date : "", location: "", description: "")

<强>输出

enter image description here

分配时的控制台

enter image description here

在SecondViewController中

enter image description here

你需要在prepareforSegue中传递indexpath

 performSegue(withIdentifier: "showDetails", sender: indexPath)

func prepareForSegue(segue: UIStoryboardSegue, sender: Any) {
            let indexPath = sender as! IndexPath
            let eventStruct =  post[indexPath.row]
            let secondViewController = segue.destination as? EventViewController
            secondViewController?.data = sender as AnyObject

        }

答案 3 :(得分:0)

第一视图控制器

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            if indexPath.row == 0
            {
                let objstory = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
            objstory.strd = imageView.image
            _ = self.navigationController?.pushViewController(objstory, animated: true)

            }
        }

<强> SecondViewController

 var strd:UIImage!
@IBOutlet weak var imgprof: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        imgprof.image = strd


        // Do any additional setup after loading the view.
    }