简体   繁体   中英

Swift - reading from array

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM