无法将数据传递给Detail View Controller Swift 3

时间:2017-05-31 08:15:07

标签: ios iphone uitableview

我正在使用Xcode 8开发一个简单的iOS 10。我刚刚开始学习Swift 3.我已经做了很多事情,只是坚持了一些我无法弄清楚的东西。我希望老年人能够清除我的概念并帮助我。

我有两个问题:

  1. 我想在tableView单元格中显示一个图像以及从firebase解析的文本。
  2. 我希望在用户触摸任何单元格时,在详细视图控制器(DetailsVC)上显示图像,标题和描述。 我创建了一个类,并且从Firebase解析了数据,我已经设法获取tableview单元格(HomeVC)上显示的数据,但是当单元格被触摸到详细视图控制器时,我无法发送数据(DetailsVC)。我想在详细视图控制器中显示图像和描述的标题(标题)。
  3. 火力地堡:

    Firebase

    DetailsVC:

    DetailsVC

    import UIKit
    import Firebase
    import FirebaseDatabase
    
    
    class HomeVC: UIViewController, UITableViewDelegate, UITableViewDataSource{
    
        var posts = [Model]()
    
    
        var ref:FIRDatabaseReference?
        var handle:FIRDatabaseHandle?
    
        @IBOutlet weak var myTableView: UITableView!
    
    
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return posts.count
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "homecell" , for: indexPath) as! TableViewCellHomeVC
    
            let post: Model
    
            post = posts[indexPath.row]
    
            cell.headingLabel.text = post.heading
    
            return cell
        }
    
    override func viewDidLoad() {
    
            ref = FIRDatabase.database().reference().child("posts")
            ref?.observe(FIRDataEventType.value, with: { (snapshot) in
                if snapshot.childrenCount > 0 {
    
                    for posts in snapshot.children.allObjects as![FIRDataSnapshot]{
                        let postsObject = posts.value as? [String: AnyObject]
                        let heading = postsObject?["heading"]
                        let image = postsObject?["image"]
                        let description = postsObject?["description"]
                        let post = Model(heading: (heading as! String?)!, image: (image as! String?)!, description: (description as! String?)!)
    
                        self.posts.append(post)
                    }
    
                    self.myTableView.reloadData()
                }
            })
    
        }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
            let details = storyboard?.instantiateViewController(withIdentifier: "DetailsVC") as! DetailsVC
    
            details.testing = posts[indexPath.row] as! String
    
    
            self.navigationController?.pushViewController(details, animated: true)
    
        }
    
        }
    

    模型

    > import Foundation
    import UIKit
    
    
    class Model {
    
        var heading:String?
    
        var image:UIImage?
    
    
    
        var description:String?
    
    
        init(heading : String,image : String,description : String) {
            self.heading = heading
            //self.image = image
            self.description = description
        }
    
    }
    

2 个答案:

答案 0 :(得分:1)

您需要传递字符串而不是模型,因为数组posts包含模型类

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let details = storyboard?.instantiateViewController(withIdentifier: "DetailsVC") as! DetailsVC
        let post: Model
         post = posts[indexPath.row]

        details.testing = post.heading as! String // if your want to pass model class object then create object in DetailsVC and pass here.
        details.objPosts = post   // this for sample if you want to pass model object    

        self.navigationController?.pushViewController(details, animated: true)

    }

}

在DetailController中

var objPosts : Model = nil // declare this property

//在viewDidLoad

let url = URL(string: objPosts.image)
let data = try? Data(contentsOf: url)

if let imageData = data {
    let image = UIImage(data: data) // assign this image to your imageview
}

答案 1 :(得分:0)

如果您想通过标题字符串,请尝试以下代码。

let post: Model
post = posts[indexPath.row]
let heading = post.heading  
details.testing = heading

感谢。