简体   繁体   中英

Swift Xcode TableViewCell's Not Loading First Time

I have a Button which Segues to my TableViewController and when I click the button for the first time, the TableView Doesn't Load but when I go back and click again, my Data Loads.

I tried to set the breakpoints but the tableview functions don't load? Any reason for this?

EDIT:

This is my Code For The TableView, if any other code is required tell me.


   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

   
    
  

}

Sorry for posting it as an answer but I am also new to stackOverFlow. It Is happening because you are performing segue before the data is fetched from the server. Perform segue as a completion after data fetch is completed. Please let me know if that was a case!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM