Как я могу использовать разные массивы для разных разделов в табличном представлении? - PullRequest
0 голосов
/ 17 февраля 2020

Я пытаюсь создать табличное представление, которое использует разные массивы для данных для каждого раздела. Но я не уверен, как получить доступ к номеру раздела, чтобы создать условия для задания данных для каждого раздела. Пожалуйста, дайте мне знать, если мое объяснение сбивает с толку, и я был бы очень признателен, если бы кто-нибудь мог мне помочь Код ниже.

import UIKit
import Firebase
import FirebaseFirestore

struct PlasticCellData {
    let image: UIImage?
    let message: String?
}
struct PlasticAlphaHeader {
    let label: String?
}

class PlasticMaterial: UITableViewController {

    var rowHeight: CGFloat = 50
    var headerheight: CGFloat = 100
    var ref: DatabaseReference?
    var databaseHandle: DatabaseHandle?

    var alphaHeaderArray = [PlasticAlphaHeader]()
    var aData = [PlasticCellData]()
    var bData = [PlasticCellData]()
    var cData = [PlasticCellData]()
    var dData = [PlasticCellData]()
    var eData = [PlasticCellData]()
    var fData = [PlasticCellData]()
    var gData = [PlasticCellData]()
    var hData = [PlasticCellData]()
    var iData = [PlasticCellData]()
    var jData = [PlasticCellData]()
    var kData = [PlasticCellData]()
    var lData = [PlasticCellData]()
    var mData = [PlasticCellData]()
    var nData = [PlasticCellData]()
    var oData = [PlasticCellData]()
    var pData = [PlasticCellData]()
    var qData = [PlasticCellData]()
    var rData = [PlasticCellData]()
    var sData = [PlasticCellData]()
    var tData = [PlasticCellData]()
    var uData = [PlasticCellData]()
    var vData = [PlasticCellData]()
    var wData = [PlasticCellData]()
    var xData = [PlasticCellData]()
    var yData = [PlasticCellData]()
    var zData = [PlasticCellData]()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        setupNavBar()
        let db = Firestore.firestore()

        alphaHeaderArray =
            [PlasticAlphaHeader.init(label: "A"), PlasticAlphaHeader.init(label: "B"),
            PlasticAlphaHeader.init(label: "C"), PlasticAlphaHeader.init(label: "D"),
            PlasticAlphaHeader.init(label: "E"), PlasticAlphaHeader.init(label: "F"),
            PlasticAlphaHeader.init(label: "G"), PlasticAlphaHeader.init(label: "H"),
            PlasticAlphaHeader.init(label: "I"), PlasticAlphaHeader.init(label: "J"),
            PlasticAlphaHeader.init(label: "K"), PlasticAlphaHeader.init(label: "L"),
            PlasticAlphaHeader.init(label: "M"), PlasticAlphaHeader.init(label: "N"),
            PlasticAlphaHeader.init(label: "O"), PlasticAlphaHeader.init(label: "P"),
            PlasticAlphaHeader.init(label: "Q"), PlasticAlphaHeader.init(label: "R"),
            PlasticAlphaHeader.init(label: "S"), PlasticAlphaHeader.init(label: "T"),
            PlasticAlphaHeader.init(label: "U"), PlasticAlphaHeader.init(label: "V"),
            PlasticAlphaHeader.init(label: "W"), PlasticAlphaHeader.init(label: "X"),
            PlasticAlphaHeader.init(label: "Y"), PlasticAlphaHeader.init(label: "Z")]


        db.collection("plastic").getDocuments { (snapshot, error) in
            if let error = error {

            }
            else {
                for document in snapshot!.documents {
                    let data = document.data()
                    let name = data["name"] as? String ?? ""
                    let firstLetter = name.prefix(1)
                    if firstLetter == "A" {
                        self.aData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "B" {
                        self.bData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "C" {
                        self.cData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "D" {
                        self.dData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "E" {
                        self.eData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "F" {
                        self.fData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "G" {
                        self.gData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "H" {
                        self.hData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "I" {
                        self.iData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "J" {
                        self.jData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "K" {
                        self.kData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "L" {
                        self.lData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "M" {
                        self.mData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "N" {
                        self.nData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "O" {
                        self.oData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "P" {
                        self.pData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "Q" {
                        self.qData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "R" {
                        self.rData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "S" {
                        self.sData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "T" {
                        self.tData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "U" {
                        self.uData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "V" {
                        self.vData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "W" {
                        self.wData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "X" {
                        self.xData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else if firstLetter == "Y" {
                        self.yData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    else {
                        self.zData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
                    }
                    self.tableView.reloadData()
                }
            }
        }

        self.tableView.register(ItemCustomCell.self, forCellReuseIdentifier: "custom")
    }

    func setupNavBar() {
        navigationItem.title = "Select Item"
        navigationController?.navigationBar.prefersLargeTitles = true
    }

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let alphaHeader = Bundle.main.loadNibNamed("AlphaHeaderView", owner: self, options: nil)?.first as! AlphaHeaderView

        alphaHeader.headerLabel.text = alphaHeaderArray[section].label
        return alphaHeader
    }
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return headerheight
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "custom") as! ItemCustomCell
        cell.itemImage = aData[indexPath.row].image
        cell.message = aData[indexPath.row].message
        cell.itemImage = bData[indexPath.row].image
        cell.message = bData[indexPath.row].message
        cell.itemImage = cData[indexPath.row].image
        cell.message = cData[indexPath.row].message
        cell.itemImage = dData[indexPath.row].image
        cell.message = dData[indexPath.row].message
        cell.itemImage = eData[indexPath.row].image
        cell.message = eData[indexPath.row].message
        cell.itemImage = fData[indexPath.row].image
        cell.message = fData[indexPath.row].message
        cell.itemImage = gData[indexPath.row].image
        cell.message = gData[indexPath.row].message
        cell.itemImage = hData[indexPath.row].image
        cell.message = hData[indexPath.row].message
        cell.itemImage = iData[indexPath.row].image
        cell.message = iData[indexPath.row].message
        cell.itemImage = jData[indexPath.row].image
        cell.message = jData[indexPath.row].message
        cell.itemImage = kData[indexPath.row].image
        cell.message = kData[indexPath.row].message
        cell.itemImage = lData[indexPath.row].image
        cell.message = lData[indexPath.row].message
        cell.itemImage = mData[indexPath.row].image
        cell.message = mData[indexPath.row].message
        cell.itemImage = nData[indexPath.row].image
        cell.message = nData[indexPath.row].message
        cell.itemImage = oData[indexPath.row].image
        cell.message = oData[indexPath.row].message
        cell.itemImage = pData[indexPath.row].image
        cell.message = pData[indexPath.row].message
        cell.itemImage = qData[indexPath.row].image
        cell.message = qData[indexPath.row].message
        cell.itemImage = rData[indexPath.row].image
        cell.message = rData[indexPath.row].message
        cell.itemImage = sData[indexPath.row].image
        cell.message = sData[indexPath.row].message
        cell.itemImage = tData[indexPath.row].image
        cell.message = tData[indexPath.row].message
        cell.itemImage = uData[indexPath.row].image
        cell.message = uData[indexPath.row].message
        cell.itemImage = vData[indexPath.row].image
        cell.message = vData[indexPath.row].message
        cell.itemImage = wData[indexPath.row].image
        cell.message = wData[indexPath.row].message
        cell.itemImage = xData[indexPath.row].image
        cell.message = xData[indexPath.row].message
        cell.itemImage = yData[indexPath.row].image
        cell.message = yData[indexPath.row].message
        cell.itemImage = zData[indexPath.row].image
        cell.message = zData[indexPath.row].message

        return cell
    }
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return rowHeight
    }

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

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return aData.count
        }
        if section == 1 {
            return bData.count
        }
        if section == 2 {
            return cData.count
        }
        if section == 3 {
            return dData.count
        }
        if section == 4 {
            return eData.count
        }
        if section == 5 {
            return fData.count
        }
        if section == 6 {
            return gData.count
        }
        if section == 7 {
            return hData.count
        }
        if section == 8 {
            return iData.count
        }
        if section == 9 {
            return jData.count
        }
        if section == 10 {
            return kData.count
        }
        if section == 11 {
            return lData.count
        }
        if section == 12 {
            return mData.count
        }
        if section == 13 {
            return nData.count
        }
        if section == 14 {
            return oData.count
        }
        if section == 15 {
            return pData.count
        }
        if section == 16 {
            return qData.count
        }
        if section == 17 {
            return rData.count
        }
        if section == 18{
            return sData.count
        }
        if section == 19 {
            return tData.count
        }
        if section == 20 {
            return uData.count
        }
        if section == 21 {
            return vData.count
        }
        if section == 22 {
            return wData.count
        }
        if section == 23 {
            return xData.count
        }
        if section == 24 {
            return yData.count
        }
        return zData.count
    }
}

1 Ответ

1 голос
/ 17 февраля 2020

В функции cellForRowAt попробуйте выбрать массив на основе раздела, например, так:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "custom") as! ItemCustomCell
        switch indexPath.section {
             case 0:
                  cell.itemImage = aData[indexPath.row].image
                  cell.message = aData[indexPath.row].message
             case 1:
                  cell.itemImage = bData[indexPath.row].image
                  cell.message = bData[indexPath.row].message
             //(...make one for each letter case until Z...)
             default:
                  cell.itemImage = zData[indexPath.row].image
                  cell.message = zData[indexPath.row].message
        }
        return cell
}

Это работает для вашей структуры данных, в идеале, хотя вы должны использовать двумерный массив с первым измерение, представляющее буквы, и второе измерение, представляющее данные каждой буквы.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...