How to make sections and put image in tableview using Firebase Realtime Database

Viewed 80

I am making an application with a base in firebase. I need to split the data into sections and cells and add a picture to the cells. but now i put data only in cells and can't add image to cell. and don't know how to make sections. This is my tree in firebase

Here is my model:

class Movie {
    var movieName: String
    var movieDuration: Int
    var movieDescription: String
    var movieGenre: String
    var movieImage: String

    init(movieName: String, movieDescription: String, movieDuration: Int, movieGenre: String, movieImage: String) {
        self.movieName = movieName
        self.movieDuration = movieDuration
        self.movieDescription = movieDescription
        self.movieGenre = movieGenre
        self.movieImage = movieImage
    }
} 

and TableViewController code:



var ref: DatabaseReference?
var movies = [Movie]()

override func viewDidLoad() {
    super.viewDidLoad()

    ref = Database.database().reference().child("Monday")
    ref?.observe(.childAdded){ [weak self](snapshot) in
        let key = snapshot.key
        guard let value = snapshot.value as? [String: Any] else {return}
        if let movieDescription = value["description"] as? String, let movieDuration = value["duration"] as? Int, let movieGenre = value["genre"] as? String, let  movieImage = value["image"] as? String {
            let movie = Movie(movieName: key, movieDescription: movieDescription, movieDuration: movieDuration, movieGenre: movieGenre, movieImage: movieImage)
            self?.movies.append(movie)
            if let row = self?.movies.count {
                let indexPath = IndexPath(row: row-1, section: 0)
                self?.tableView.insertRows(at: [indexPath], with: .automatic)
            }
        }
    }
}

override func numberOfSections(in tableView: UITableView) -> Int {
    return 0
}

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let movie = movies[indexPath.row]
    let cell = tableView.dequeueReusableCell(withIdentifier: "MovieCell", for: indexPath)
    cell.textLabel?.text = movie.movieName
    cell.textLabel?.textColor = .white
    cell.detailTextLabel?.text = movie.movieGenre
    cell.detailTextLabel?.textColor = .white

    return cell
}

if someone can help me with this it will be great...)

1 Answers

welcome to StackOverflow. I believe your problem is strictly related to the UITableView logic. You are not able to make tableView Sections because in the delegate method override func numberOfSections(in tableView: UITableView) -> Int you return 0. That means you have zero sections. If you want more than one section, you need to change the method to:

override func numberOfSections(in tableView: UITableView) -> Int{
     return 2 // this gives you two sections
}

Also, you need to specify how many rows per section like this

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0{
         return 5 //you have five rows in the first section
    }else { //second section
         return 7 // you have seven row in the second section 
    }
}

Now we come to the second part - how to have an image. The problem is that to have an image you need to create a custom cell, a subclass of UITableViewCell, that contains an imageView. This is not hard to implement but there are multiple steps to do, with great online tutorials though :). I suggest you look online for UITableView custom cells tutorial.

Related