【问题标题】:Retrieving user's Info from firebase based on project name and Details-Swift Project根据项目名称和Details-Swift Project从firebase中检索用户信息
【发布时间】:2016-08-05 16:42:36
【问题描述】:

这是使用 swift 和 firebase 的 iOS 应用程序的一些用户的仪表板结构:

{
"users" : {
"0a0462bb-86a1-4140-bda2-90a62236ab1e" : {
  "Picker" : "Jeddah",
  "details" : "I'm here",
  "email" : "y@gmail.com",
  "name" : "y",
},
"0b71693a-4f16-4759-acd6-706da1a466a0" : {
  "Picker" : "Jubail",
  "details" : "iOS second",
  "email" : "Siham0@gmail.com",
  "name" : "Siham",
},
"0b71693a-4f16-4759-acd6-706da1a466a0" : {
  "Picker" : "Jeddah",
  "details" : "",
  "email" : "roro0@gmail.com",
  "name" : "roro",
}
 }
} 

如下图:

模拟器中的表格视图包含选择器为 Jeddah 的所有用户的名称和详细信息。

我现在想要的是在用户单击任何单元格时检索另一个视图控制器中标签中的名称、电子邮件和详细信息!

例如,如果我单击第一个单元格,第二个视图控制器将具有以下内容:

名称: 是的

详情:我来了

电子邮件: y@gmail.com

我应该怎么做才能让它发挥作用?和 我应该使用什么结构的检索函数?

【问题讨论】:

  • 为什么没有人回答这个问题? :(
  • 问题不清楚。您的第一个屏幕截图显示 y 已被点击,但详细视图中的数据不匹配。就像上一个屏幕截图中的数据一样:数据集中的电子邮件是 n@g.com,但屏幕截图显示的是 y@g.com。有相关性吗?此外,您提到您查询 Picker is Jeddah,但是,该代码不包括在内。最后,发布的观察事件将加载用户节点中的所有内容,遍历所有子节点,快速连续地替换视图中的 .text 值。如果您能澄清问题并将其清理干净,我们可以提供帮助。
  • 执行我的英语,因为它不是我的母语。
  • 我编辑了这个问题并试图尽可能地澄清它。我希望你现在得到我想要的。

标签: ios xcode swift firebase data-retrieval


【解决方案1】:

我确实通过简单的格式制作了一个包含所有列表值的数组,并显示如下项目列表:

var namesArray : NSMutableArray = []

它是viewDidLaod 方法如下所示:

 override func viewDidLoad() {
        super.viewDidLoad()



        ref.queryOrderedByChild("Picker").queryEqualToValue("Makkah")
                    .observeSingleEventOfType(.Value, withBlock: { snapshot in


                        if let dict = snapshot.value as? NSMutableDictionary{
                            print("print \(dict)")


                            for (key,value) in dict {
                                 let mainDict = NSMutableDictionary()
                                mainDict.setObject(key, forKey: "userid")


                                 if let dictnew = value as? NSMutableDictionary{

                                    if let metname = dictnew["name"] as? String
                                    {
                                        mainDict.setObject(metname, forKey: "name")

                                    }
                                    if let metname = dictnew["Picker"] as? String
                                    {
                                        mainDict.setObject(metname, forKey: "Picker")

                                    }
                                    if let metname = dictnew["details"] as? String
                                    {
                                        mainDict.setObject(metname, forKey: "details")

                                    }
                                    if let metname = dictnew["email"] as? String
                                    {
                                        mainDict.setObject(metname, forKey: "email")

                                    }
                                    if let metname = dictnew["provider"] as? String
                                    {
                                        mainDict.setObject(metname, forKey: "provider")

                                    }

                                }
                                self.namesArray.addObject(mainDict)
                        }

                            print("arrayt is \(self.namesArray)")


                    }

                        self.CookingTableView.reloadData()
                        })


    }

其数组Show在TableView的cellForRowAtIndexPath中必须有如下代码:

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

        let cell = self.CookingTableView.dequeueReusableCellWithIdentifier("CookingCell", forIndexPath: indexPath) as! CookingTableViewCell

        if let name = namesArray[indexPath.row] as? NSMutableDictionary{

             cell.BusinessNameLabel?.text = name["name"] as? String
            cell.DescriptionLabel?.text = name["details"] as? String
        }

        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

        return cell

    }

现在上市的事情已经完成了。让我们在didSelectRowAtIndexPath 方法上显示它的详细信息:

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {


                 tableView.deselectRowAtIndexPath(indexPath, animated: true)
                 dispatch_async(dispatch_get_main_queue()) { [unowned self] in

                    let DetailsViewController = self.storyboard!.instantiateViewControllerWithIdentifier("ProjectDetailsViewController") as! ProjectDetailsViewController

                    if let name = self.namesArray[indexPath.row] as? NSMutableDictionary{

                        DetailsViewController.self.strUserid = name["userid"] as? String
                    }


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

        }
    }

在上面的 didSelect 方法中,我传递了 userID 唯一用户 ID 需要在 PushedViewController 中获取它的详细信息。您还可以将选定项目的墙字典发送到 nextviewController 但我在 nextViewController 中使用调用的 firebase 查询方法,因此 nextViewController 代码如下所示:

var strUserid : NSString!

它是 ViewDidLoad:

 override func viewDidLoad() {
        super.viewDidLoad()

        print("idis \(self.strUserid)")


      ref.childByAppendingPath(self.strUserid as String).observeEventType(.Value, withBlock: { snapshot in


         if let dict = snapshot.value as? NSMutableDictionary{

            print("dict is \(dict)")

            if let name = dict["name"] as? String
            {
               self.Name.text = name

            }

            if let details = dict["details"] as? String
            {
                self.Details.text = details

            }

            if let email = dict["email"] as? String
            {
                self.Email.text = email

            }

        }



            })
    }

就是这样,希望您的问题能够得到解决。我用上面的方法做了这种方法。

【讨论】:

    【解决方案2】:

    由于您有一个 tableView 显示来自 Firebase 的一些数据,我假设您已经成功调用您的 Firebase 并获取数据。

    最好的方法是编写一个自定义类 User 或适当命名的东西,您可以在其中存储所有详细信息在对 Firebase 的初始调用中。

    然后,在您的tableView 中,您可以只在其中显示您想要的数据,当点击一个单元格时,您通过prepareForSegue 方法将该User 类的实例发送到下一个ViewController

    在下一个ViewController 中,您现在可以访问该User 的所有数据,因此您可以在该页面上显示您需要/想要显示的任何内容。

    希望这会有所帮助。

    编辑:

    User 类或您选择的任何名称都将是一个单独的 Swift 文件。 Here's an exampleMovie 类我写来跟踪电影数据。

    你可以有一个你的类的数组:var movieArray = [Movie]()

    然后您可以浏览所有 Firebase 数据,创建您的类的新实例并附加到您的数组中。

    func loadMovies() {
            print("Loading movies now")
            currentUserMovies.observeEventType(.Value) { (snapshot: FDataSnapshot!) in
                let json = JSON(snapshot.value)
                var movieArr = [Movie]()
                print(json)
                for (key, subJson) in json {
                    let name = key
                    let userrating = subJson["User Rating"].int!
                    let imagePath = subJson["Image"].string!
                    let plot = subJson["Plot"].string! ?? "No Plot Found!"
                    let imdbRating = subJson["IMDB Rating"].string!
                    var image = UIImage()
                    if let url = NSURL(string: imagePath) {
                        if let data = NSData(contentsOfURL: url) {
                            image = UIImage(data: data)!
                        }
                    }
                    let movie = Movie(name: name, rating: userrating, image: image, plot: plot, imdbRating: imdbRating)
                    movieArr.append(movie!)
                }
                self.myArray = movieArr
            }
        }
    

    我在这里使用SwiftyJSON 来解析数据。你可以在他们的 Github 上找到如何做到这一点的示例。

    您将在 TableViewController 中拥有您的 prepareForSegue 方法。 示例:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
            // Get the new view controller using segue.destinationViewController.
            // Pass the selected object to the new view controller.
            print("Preparing")
            if (segue.identifier == "showSearchResult")
            {
                if let destinationViewController = segue.destinationViewController as? DisplayMovieViewController {
                    destinationViewController.plot = self.moviePlot
                    destinationViewController.imdbRating = self.movieRating
                    destinationViewController.name = self.movieName
                    destinationViewController.imagePath = self.moviePicturePath
    
                }
            }
    
        }
    

    我在DisplayMovieViewController 中声明了属性plot, imagePath, name and imdbRatingprepareForSegue() 方法在 segue 发生之前设置这些值。

    Here is my project 如果您需要更多示例/示例代码来查看。如果需要,请下载并运行它。

    【讨论】:

    • 编辑了我的答案。现在检查一下,看看是否有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-11
    • 2017-05-29
    • 2019-07-18
    • 2014-04-06
    • 2023-03-27
    • 2015-05-31
    • 2016-06-23
    相关资源
    最近更新 更多