如何使用Resizable单元格在tableView中显示Firebase中的图像?

时间:2017-03-30 02:21:24

标签: swift image uitableview

我有一个tableView可以从Firebase获取信息。抓取的内容是文字和照片。我遵循了这个教程:https://www.raywenderlich.com/129059/self-sizing-table-view-cells我按照了“告诉我艺术”一节。对于图像视图,我使用了Aspect Fit。文本首先加载然后加载图片,但图像显示在错误的位置,因为它不在视图中,我必须滚动很长的路。我尝试过使用估计的细胞高度,但它似乎没有帮助。所以TLDR:我使用带有imageView和标签的自定义tableView单元格,这两个单元格都来自Firebase的数据。单元格是autoResizing。加载图片前的文字图片:Image of Cell before Image Loads以下是图片加载后的结果:This is after the image has loaded. The image is nowhere in site and the text has been pushed down.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! BooksTableViewCell

    // Configure the cell...

    let bookSnapShot: FIRDataSnapshot! = self.SecondResultArray[indexPath.row]

    let book = bookSnapShot.value as! Dictionary<String, String>

    let Author = book["Author"] as String!
    let Comment = book["Comment"] as String!
    let Genre = book["Genre"] as String!
    let User = book["User"] as String!
    let title = book["title"] as String!
    let bookPhoto = book["bookPhoto"] as String!
    let userID = book["userID"] as String!
    let userLocation = book["userLocation"] as String!
    let bookRate = book["bookRating"] as String!
    let userToBePushed = book["pushingID"] as String!
    let bookLat = book["bookLat"] as String!
    let bookLng = book["bookLng"] as String!

    if bookRate == "Age Restricted"{


        cell.bookLabel?.numberOfLines = 0
        cell.bookLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping



        cell.bookLabel?.text = "This book is age restricted that is why you are seeing a default image"
  + "\n" + "Title: " + title! as String + "\n" + "Author: " + Author! as String + "\n" + "Comment: " + Comment! as String + "\n" + "Genre: " + Genre! as String + "\n" + "User: " + User! as String + "\n" + "Location: " + userLocation! as String



    cell.bookImageView?.image = UIImage(named: "old-books-436498_640.jpg")?.circleMask


    }
    else {

    let url = URL(string: bookPhoto!)



    DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.default).async {


        let data = try? Data(contentsOf: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check
        DispatchQueue.main.async(execute: {

            let images = UIImage(data: data!)


            cell.bookImageView!.image = UIImage(data: data!)


            self.tableView.reloadData()
        });
    }




    cell.bookLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping

  cell.bookLabel?.text = "Title: " + title! as String + "\n" + "Author:    " + Author! as String + "\n" + "Comment: " + Comment! as String + "\n" + "Genre: " + Genre! as String + "\n" + "User: " + User! as String + "\n" + "\n" + "Location: " + userLocation! as String





    }

    cell.backgroundColor = .clear

   cell.bookLabel?.textColor = UIColor.white

    cell.bookLabel?.font = UIFont(name: "Chalkduster", size: 20)

    return cell
}

0 个答案:

没有答案