获取TableviewCells以显示从firebase中提取的不同数据并在CollectionView中显示它们

问题描述 投票:0回答:2

我有这两个结构,我从Firebase Database获取数据,我不知道如何获取每个tableview单元以显示不同的集合视图,我在xib文件中同时拥有两个单元(TableviewCell和CollectionViewCell),但是无法以我想要的方式获得它们。

我已将我的ViewController附加在其中,并附加了TableViewCell类。请帮助

struct Clothes {
var price: Double
var imgClUrl: String
var id: String
var isActive: Bool = true
var timeStamp: Timestamp
var stock: Double
var name: String
init(data : [String : Any]) {
    self.price = data["price"] as? Double ?? 0
    self.imgClUrl = data["imgClUrl"] as? String ?? ""
    self.id = data["id"] as? String ?? ""
    self.isActive = data["isActive"] as? Bool ?? true
    self.timeStamp = data["timeStamp"] as? Timestamp ?? Timestamp()
    self.stock = data["stock"] as? Double ?? 0
    self.name = data["name"] as? String ?? ""
}

}

struct TypesOfCLothing {
var title : String
var id : String
var clothes : [Clothes]
init (data : [String : Any]){
    self.title = data["title"] as? String ?? ""
    self.id = data["id"] as? String ?? ""
    self.clothes = data["Clothes"] as! [Clothes]
}

}

class eachStoreTableViewCell: UITableViewCell{

@IBOutlet weak var eachRowCollView: UICollectionView!

@IBOutlet weak var eachRowLbl: UILabel!


var clothes = [Clothes]()
override func awakeFromNib() {
    super.awakeFromNib()


    eachRowCollView?.reloadData()
    eachRowCollView?.delegate = self
    eachRowCollView?.dataSource = self
    self.eachRowCollView?.register(UINib(nibName: "RowColectionView", bundle: nil), forCellWithReuseIdentifier: Identifiers.ClothesCell)
}
override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
}

func configureCell(clothingType: TypesOfCLothing) {
    eachRowLbl?.text = clothingType.title}}
extension eachStoreTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return clothes.count
}



func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Identifiers.ClothesCell, for: indexPath) as? RowColectionView{
        cell.configureCell(clothes: clothes[indexPath.item])
        return cell
    }
    return UICollectionViewCell()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: 160, height: 180)
}


class EachStoreVC: UIViewController {


@IBOutlet weak var StoreTblView: UITableView!

 var typesOfClothing = [TypesOfCLothing]()

var tienda: Tiendas!
let db = Firestore.firestore()
var listener : ListenerRegistration!
        override func viewDidLoad() {
        super.viewDidLoad()

  // StoreTblView?.delegate = self
   // StoreTblView?.dataSource = self
            StoreTblView?.register(UINib(nibName: "eachStoreTblViewCell", bundle: nil), forCellReuseIdentifier: Identifiers.ClothingTypeCell)
            StoreTblView?.cellLayoutMarginsFollowReadableWidth = false
}
override func viewDidAppear(_ animated: Bool) {

}
override func viewDidDisappear(_ animated: Bool) {

}

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


extension EachStoreVC : UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return typesOfClothing.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let cell = tableView.dequeueReusableCell(withIdentifier: Identifiers.ClothingTypeCell, for: indexPath) as? eachStoreTableViewCell {
        cell.configureCell(clothingType: typesOfClothing[indexPath.row])
        cell.frame = CGRect(x: 0, y: 0, width: StoreTblView.frame.size.width, height: cell.frame.size.height)
        return cell
        }
    return UITableViewCell()
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    return 200
 }}

编辑现在,collectionView和Tableview可以正常显示,但是我无法从firebase中获取数据,每个TypesOfClothing文档中都有一个文档引用数组,代码如下。我还更新了我的2种结构。

    func getRowData(){
   listener = db.collection("TypesOfClothing").addSnapshotListener({ (snap, error) in
        if let error = error {
            debugPrint(error.localizedDescription)
            return
        }
        snap?.documentChanges.forEach({ (change) in
            let data = change.document.data()
            let typesOfClothing = TypesOfCLothing.init(data: data)

            switch change.type {
                case.added:
                    self.onDocumentAdded(change: change, category: typesOfClothing)
                case.modified:
                    self.onDocumentModified(change: change, category: typesOfClothing)
                case.removed:
                    self.onDocumentRemoved(change: change)

            }
        })

    })    }


func onDocumentAdded(change: DocumentChange, category: TypesOfCLothing){
    let newIndex = Int(change.newIndex)
    typesOfClothing.insert(category, at: newIndex)
    StoreTblView?.insertRows(at: [IndexPath(row: newIndex, section: 0)], with: UITableView.RowAnimation.left)

}
func onDocumentModified(change: DocumentChange, category: TypesOfCLothing){
    if change.newIndex == change.oldIndex {
        let index = Int(change.newIndex)
        typesOfClothing[index] = category
        StoreTblView?.reloadRows(at: [IndexPath(row: index, section: 0)], with: UITableView.RowAnimation.left)

    } else {
        let oldIndex = Int(change.oldIndex)
        let newIndex = Int(change.newIndex)
        typesOfClothing.remove(at: oldIndex)
        typesOfClothing.insert(category, at: newIndex)

        StoreTblView?.moveRow(at: IndexPath(row: oldIndex, section: 0), to: IndexPath(row: newIndex, section: 0))
    }
}
func onDocumentRemoved(change: DocumentChange){
    let oldIndex = Int(change.oldIndex)
    typesOfClothing.remove(at: Int(oldIndex))
    StoreTblView?.deleteRows(at: [IndexPath(row: oldIndex, section: 0)], with: UITableView.RowAnimation.fade)
}
ios swift firebase uitableview uicollectionviewcell
2个回答
0
投票

像这样的结构用法:

struct TypesOfClothing {
   var title : String
   var clothes: [Clothes]

}

struct Clothes {
 var price: Double
 var imgClUrl: String
 var id: String
 var isActive: Bool = true
 var timeStamp: Timestamp
 var stock: Double
 var name: String
}

在Main View控制器中:尝试这样的代码:

var typesOfClothing: [TypesOfClothing] = []

in numberOfRowsInSection

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

在cellForRowAt

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     if let cell = tableView.dequeueReusableCell(withIdentifier: Identifiers.ClothingTypeCell, for: indexPath) as? eachStoreTableViewCell {

        if indexPath.row < typesOfClothing?.count ?? 0 {
           let cellData = typesOfClothing?[indexPath.row]

           cell.configureCell(cellData)
        }
       return cell
    }        
    return UITableViewCell()
  }

在UITableViewCell类中尝试这样:

class eachStoreTableViewCell: UITableViewCell{

   @IBOutlet weak var eachRowCollView: UICollectionView!

   @IBOutlet weak var eachRowLbl: UILabel!


   var clothes = [Clothes]()
   override func awakeFromNib() {
        super.awakeFromNib()

       eachRowCollView?.delegate = self
       eachRowCollView?.dataSource = self
       self.eachRowCollView?.register(UINib(nibName: "RowColectionView", bundle: nil), forCellWithReuseIdentifier: Identifiers.ClothesCell)
       }
       override func setSelected(_ selected: Bool, animated: Bool) {
         super.setSelected(selected, animated: animated)
       }

       func configureCell(_ clothingType: TypesOfClothing) {
        eachRowLbl?.text = clothingType.title
        clothes = clothingType.clothes 
        eachRowCollView?.reloadData()
         }
     }

   extension eachStoreTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
     return clothes.count
  }


  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Identifiers.ClothesCell, for: indexPath) as? RowColectionView{
    cell.configureCell(clothes: clothes[indexPath.item])
    return cell
    }
     return UICollectionViewCell()
 }
 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 160, height: 180)
      }
  }

它将为您提供帮助。


0
投票

您可以为加载表查看数据创建一个结构

struct ClothingData
{
   var typesOfClothing : String = ""
   var clothes : [Clothes] = []
}

为此tabArrayView加载此structArray

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: Identifiers.ClothingTypeCell, for: indexPath) as? eachStoreTableViewCell

    let row = ClothingData[indexPath.row]

    cell.eachRowLbl?.text = row.typesOfClothing
    cell.clothes = row.clothes
    // Here you can pass clothesList to tabelViewCell for load collectionView
    return cell
}
© www.soinside.com 2019 - 2024. All rights reserved.