繁体   English   中英

如何使用 Firebase 实时数据库制作部分并将图像放入表视图中

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

我正在制作一个以 firebase 为基础的应用程序。我需要将数据拆分为多个部分和单元格,并向单元格添加图片。 但现在我只将数据放在单元格中,无法将图像添加到单元格中。 并且不知道如何制作部分。 这是我在 firebase 的树

这是我的 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
    }
} 

和 TableViewController 代码:



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
}

如果有人可以帮助我,那就太好了……)

欢迎来到 StackOverflow。 我相信您的问题与UITableView逻辑严格相关。 您无法制作tableView部分,因为在委托方法中override func numberOfSections(in tableView: UITableView) -> Int您返回 0。这意味着您有零部分。 如果你想要一个以上的部分,你需要将方法更改为:

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

此外,您需要像这样指定每个部分的行数

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 
    }
}

现在我们来到第二部分——如何拥有图像。 问题是要获得图像,您需要创建一个自定义单元格,它是UITableViewCell的一个子类,其中包含一个 imageView。这并不难实现,但需要执行多个步骤,不过有很棒的在线教程:)。 我建议你上网找 UITableView 自定义单元格教程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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