gpt4 book ai didi

ios - 尝试使 numberOfRowsInSection 计数等于 Swift 中另一个文件的数组计数

转载 作者:行者123 更新时间:2023-11-30 11:17:06 25 4
gpt4 key购买 nike

目前,我正在从 firebase 获取数据,将数据转换为对象数组,并尝试使该数组计数成为“numberOfRowsInSection”的计数。因此,我在声明数组的 View Controller 上得到了正确的计数,但当我将该数据传递给 TableView Controller 时,我不断得到 0。

下面我展示了 View Controller ,我在其中获取数据并将其放入过滤数组中:

 func fetchAllData( completion: @escaping (_ call: [Restaurant]) -> Void) {

self.ref = Database.database().reference()
self.ref?.observe(.value, with: { (snap) in
guard let topArray = snap.value as? [[String:Any]] else {print(":(") ; return }
var restaurantArray = [Restaurant]()

for dictionary in topArray {
self.restaurantGroup.enter()
guard let address = dictionary["Address"] as? String,
let city = dictionary["City"] as? String,
let inspectionDate = dictionary["Inspection Date"] as? String,
let name = dictionary["Name"] as? String,
let major = dictionary["Number of Occurrences (Critical Violations)"] as? Int,
let minor = dictionary["Number of Occurrences (Noncritical Violations)"] as? Int,
let violationTitle = dictionary["Violation Title"] as? String else { continue }
print(2)
//MARK: - creates restaurants from the list above
let restaurant = Restaurant(address: address, city: city, inspectionDate: inspectionDate, name: name, major: major, minor: minor, violationTitle: violationTitle)

print(3)
//MARK: - Adds a restaurant to restaurant array instance
restaurantArray.append(restaurant)
}
self.restaurantList = restaurantArray
self.restaurantGroup.leave()
completion(self.restaurantList)

let dictionaryNew = Dictionary(grouping: self.restaurantList) { $0.name + " " + $0.address}

self.restaurantListModified = dictionaryNew
print(self.restaurantListModified.count)
})
}

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

var filteredArray = [[Restaurant]]()
guard let userSearch = searchBar.text?.uppercased() else { return }
pulleyViewController?.setDrawerPosition(position: .partiallyRevealed, animated: true)

var nameArray = [String]()

for (key, value) in restaurantListModified {

if value[0].name.hasPrefix(userSearch.uppercased()){
filteredArray.append(value)
}
}

for subarray in filteredArray {
let nameArrayForTBView = subarray[0].name
nameArray.append(nameArrayForTBView)
}
self.tableViewNameArray = nameArray
print("\(tableViewNameArray.count)😋😋😋😋😋😋😋😋😋😋😋😋")
print("this First")
}
}

tableViewNameArray 是我尝试传递给此 tableView Controller 的数组

   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

print("This second")
print(MapViewController.sharedMapViewController.tableViewNameArray.count)

return MapViewController.sharedMapViewController.tableViewNameArray.count

}

我还将我的数组设置为在 tableView Controller 中接收的通知中心,如下所示:

var tableViewNameArray = [String]() {

didSet {
DispatchQueue.main.async {
DispatchQueue.main.async {
NotificationCenter.default.post(name: MapViewController.RestaurantNotification.notificationSet, object: self)
}
}
}
}

积分:

  1. 我的 View Controller 数组在 View Controller 上获得了正确的计数,但准确的计数没有传递到 tableView Controller 。
  2. 传递的数组的 tableview 计数为 0

最佳答案

我发现了@Kamran 指出的问题。我没有分配 self.tableViewNameArray = nameArray。所以我改变了

var tableViewNameArray to 
static var tableViewNameArray

如果您像我一样是编程新手,请确保您阅读更多有关局部变量和全局变量的内容。很有帮助。

关于ios - 尝试使 numberOfRowsInSection 计数等于 Swift 中另一个文件的数组计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51644591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com