0
let custom = Bundle.main.loadNibNamed("PostView", owner: self, options: nil)?.first as! PostView
custom.layer.cornerRadius = customCard.bounds.width/64
custom.textLabel.text = "number \(index)"
custom.numberOfComment.text = "4"
custom.ratingLabel.text = "+ 39"
custom.ratingLabel.textColor = UIColor(red:0.61, green:0.92, blue:0.53, alpha:1.0)

            return custom

What I am trying to do is to set text of a seval labels in a separate .xib file, with an array created with code shown bellow.

var posts:[Post] = []

func observeDatabase() {

    let Ref = FIRDatabase.database().reference().child("posts")
    Ref.observe(.childAdded, with: { snapshot in

        let snapshotValue = snapshot.value as? NSDictionary

        if let title = snapshotValue?["title"] as? String {

            let postText = snapshotValue?["text"] as? String
            let postName = snapshotValue?["name"] as? String
            let postNumberOfComments = snapshotValue?["numberOfComments"] as? NSNumber
            let postRating = snapshotValue?["rating"] as? NSNumber
            let postID = snapshotValue?["postID"] as? String

            self.posts.append(Post(name: postName as NSString?, image: UIImage(named: "none"), text: postText as NSString?, title: title as NSString?, comments: postNumberOfComments, rating: postRating, postID: postID))
        }
    })
}

And with struct

struct Post {

            let Name: NSString
            let Image: UIImage! // Should be optional in the future
            let Title: NSString // Title
            let Text: NSString // Text
            let Comments: NSNumber? // Number of comments
            let Rating: NSNumber // Number of up/down -votes
            let PostID: String

            var description: String {
                return "Name: \(Name), \n Image: \(Image), Text: \(Text), Title: \(Title), Comments: \(Comments), Rating: \(Rating), PostID: \(PostID)"
            }

            init(name: NSString?, image: UIImage?, text:NSString?, title: NSString?, comments: NSNumber?, rating: NSNumber?, postID:String?) {
                self.Name = name ?? ""
                self.Image = image
                self.Text = text ?? ""
                self.Title = title ?? ""
                self.Comments = comments ?? 0
                self.Rating = rating ?? 0
                self.PostID = postID ?? ""
            }   
}

Question : How do I read the array and set the text of the labels according to its indexpath? Thanks!

1 Answer 1

2

Assuming you have a tableView in your .xib file due to your reference to indexPath:

You can extract the post for a certain row using:

let post = posts[indexPath.row]

Assuming you have a UILabel named nameLabel you can set it's text property using:

nameLabel.text = post.Name
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.