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

标签 ios arrays swift uitableview

目前,我正在从 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/

相关文章:

swift - OS X 上的 SystemStatusBar statusItem 标题被缩短

ios - 从自定义 UITableViewCell 保存数据

java - Java 中的 IntegerList 帮助

ios - 来自 PHImageManager 的图像数据不同于保存为 Assets 的图像数据

java - 如何获取没有设定长度的数组的特定组成部分?

java - 从 ArrayList<Integer> 创建 int[]

ios - 不同图像方向和宽高比的 CollectionView 布局?

ios - 带有 MapView 和 TableView 的模态视图 Controller

ios - 更改UILabel.text的字符串

iphone - 使用 ARC 重新初始化/重新分配对象