Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

swift - connecting Firebase to tableview

I'am using this code to make collection view inside table view

import UIKit

struct bookS {
    
    let title : String
    let photos : [UIImage]
}
class TablecollViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
   
   var bookCollection = [bookS]()

    
    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.delegate = self
        tableView.dataSource = self

    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return bookCollection.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "tblCell") as! tableCollCell
        
 
        
        let book = bookCollection[indexPath.row]
        cell.setupCell(title: book.title, arrPhotos: book.photos)
        return cell
    }
    
 
    
   
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 200
        
    }
}




class tableCollCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    

    
    @IBOutlet weak var lblTitle: UILabel!
    
 
    @IBOutlet weak var collectionView: UICollectionView!
    
    var arrPhotos = [UIImage]()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        collectionView.delegate = self
        collectionView.dataSource = self
    }
    

    
    public func setupCell(title: String, arrPhotos: [UIImage]) {
        lblTitle.text = title
        self.arrPhotos = arrPhotos
        
    }


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return arrPhotos.count
}


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "colCell", for: indexPath) as! collCell
    

    
    cell.imageView.image = arrPhotos[indexPath.row]
    

    
    
    return cell
}

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        CGSize(width: self.collectionView.frame.width * 0.33, height: self.collectionView.frame.width * 0.45)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0.2
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0.1
    }
}

class collCell: UICollectionViewCell {
    
    
    @IBOutlet weak var imageView: UIImageView!
    
}

and usually, I use this code to connect with Firebase



  listener = Firestore.firestore().collection(DatabaseService.booksCollection).addSnapshotListener({ [weak self] (snapshot, error) in
        
        
        if let error = error {
          DispatchQueue.main.async {
            self?.showAlert(title: "Try again later", message: "(error.localizedDescription)")
          }
        } else if let snapshot = snapshot {
         
            let bookSnap = snapshot.documents.map { booksM($0.data())}
          
            self?.booksCollection = bookSnap
        }
      })

class DatabaseService {
    
    static let booksCollection = "books" // collection

    
    private let db = Firestore.firestore()
}

and this in cell subclass

    public func configureCell(for book: booksM) {
            ColImage.kf.setImage(with: URL(string: book.imageURLM))
            ColTitle.text = book.bookNameM

I usually use it with tableView alone or collectionView alone but since I combined tableView with collectionView I couldn't figure out how to use the usual codes. especially with an array of Images var arrPhotos = UIImage in the UITableViewCell and this code

 ColImage.kf.setImage(with: URL(string: book.imageURLM))

ColImage is usually a single image but now I deal with an array of images.

help is appreciated

question from:https://stackoverflow.com/questions/65557352/connecting-firebase-to-tableview

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...