我如何在闭包之外访问数组?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我如何在闭包之外访问数组?相关的知识,希望对你有一定的参考价值。
我是使用Cloud Firestore在ios应用中获取数据的初级开发人员,希望读取数据并将其存储在数组中。我在函数外部声明了数组以读取数据,但是如果不使用“ self”,就无法访问函数内部的数组。但是,然后数据不存储在数组中。我想知道如何将值存储在数组中,同时还可以在函数外部访问它们。我知道这可能是一个非常简单的问题,但是如果有人可以帮助我,我将不胜感激。这是代码。
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)]
}
if firstLetter == "B" {
self.bData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "C" {
self.cData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "D" {
self.dData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "E" {
self.eData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "F" {
self.fData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "G" {
self.gData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "H" {
self.hData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "I" {
self.iData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "J" {
self.jData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "K" {
self.kData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "L" {
self.lData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "M" {
self.mData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "N" {
self.nData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "O" {
self.oData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "P" {
self.pData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "Q" {
self.qData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "R" {
self.rData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "S" {
self.sData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "T" {
self.tData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "U" {
self.uData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "V" {
self.vData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "W" {
self.wData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "X" {
self.xData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "Y" {
self.yData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
if firstLetter == "Z" {
self.zData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
}
}
}
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 = 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
}
}
答案
在设置数据的最后一行之后,告诉表视图重新加载。
if firstLetter == "Z" {
self.zData = [PlasticCellData.init(image: #imageLiteral(resourceName: "PlasticSilverware"), message: name)]
}
tableView.reloadData()
现在将有数据,表格将能够显示它。
以上是关于我如何在闭包之外访问数组?的主要内容,如果未能解决你的问题,请参考以下文章