【问题标题】:Swift Xcode TableViewCell's Not Loading First TimeSwift Xcode TableViewCell 没有第一次加载
【发布时间】:2020-10-16 09:40:14
【问题描述】:

我有一个链接到我的TableViewController 的按钮,当我第一次单击该按钮时,TableView 不会加载,但是当我返回并再次单击时,我的数据会加载。

我尝试设置断点,但无法加载 tableview 函数?这有什么原因吗?

编辑:

这是我的 TableView 代码,如果需要任何其他代码,请告诉我。


   class ServicesDisplay: UITableViewController {

    @IBOutlet var MainTitle: UILabel!
    
    @IBOutlet var image: UIImageView!
    let db = Firestore.firestore()

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        myIndex = indexPath.row
        performSegue(withIdentifier: "seque", sender: self)
        
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 260
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let jsonFbPic = (jsonFile["SecondScreen"])
            let test = ((jsonFbPic["Services Image"]))
        let count = ((test["Image\(myIndex)"]))
        
        
        if count.count == 0 {
            
            return serviceTextArray.count
            
        } else {
        
        return count.count

        }
    }

    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomServicesCell
        
        cell.serviceText?.text = serviceTextArray[indexPath.row]
       
        //Variables
        let Json = jsonFile["SecondScreen"]
        let MainTitleJson = Json["Text"]
        let MainTitleSize = CGFloat(Int("\(MainTitleJson["Size"])")!)
        //Main Title:
        cell.serviceText.font = UIFont(name: "\(MainTitleJson["Font"])", size: MainTitleSize)
        cell.serviceText.textColor = hexStringToUIColor(hex: "\(MainTitleJson["Color"])")
        cell.serviceImage?.loadImagesFromCacheWithUrlString(myIndex: indexPath.row, labelName: serviceTextArray[indexPath.row], CellImage: cell.serviceImage)
        cell.selectionStyle = .none
        
        return cell
    }
    
    
    
        
    
    public func hexStringToUIColor(hex: String) -> UIColor {
          var cString: String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
          var rgbValue: UInt64 = 0
          if cString.hasPrefix("#") {
              cString.remove(at: cString.startIndex)
          } else if cString.count != 6 {
              return UIColor.black
          }
          Scanner(string: cString).scanHexInt64(&rgbValue)
          return UIColor(red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0, green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0, blue: CGFloat((rgbValue & 0x0000FF)) / 255.0, alpha: CGFloat(1.0))
      }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        DispatchQueue.main.async(execute: { () -> Void in
            self.tableView.reloadData()
        })
    }
    override func viewDidAppear(_ animated: Bool) {
        
    }
    

    
    // MARK: - Table view data source

   
    
  

}

【问题讨论】:

  • 你能告诉我们你的代码吗?
  • 你能确保 serviceTextArray.count 不返回零吗?
  • @user13613549 - 请查看How to Ask。您已经展示了一些非常不完整的代码。您的代码显示了 2 个未出现在您的代码中的 @IBOutlets,并声明了 let db = Firestore.firestore(),它也没有在任何地方显示......并且您的代码正在使用 jsonFileserviceTextArray - 两者都没有定义或设置在任何地方。

标签: ios swift xcode uitableview


【解决方案1】:

很抱歉将其发布为答案,但我也是 stackOverFlow 的新手。发生这种情况是因为您在从服务器获取数据之前执行了 segue。在数据获取完成后执行 segue 作为完成。如果是这样,请告诉我!

【讨论】:

    猜你喜欢
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多