2017-06-26 26 views
0

我試圖獲得一個表視圖來加載一些我從imgur獲得的JSON數據。我可以向後端發出一個請求,獲取imgur數據,將其吐出給我,然後填充表格。然而,最後我會發現這個數據截止的真正奇怪的問題。 Here是一張圖片。如果我嘗試再做一次搜索,它不會將更多數據加載到表格中,所以當我這樣做時很明顯我會分解它。我真的不知道我做錯了什麼。我也試圖在某些時候使用模態來點擊它,並且將可重用細胞出列也可能不正確,但我想我會在出現這個問題後到達那裏。添加JSON數據時UITableView無法正常工作

無論如何,任何幫助,將不勝感激。

class ViewController3: UIViewController, UITextFieldDelegate,UITableViewDataSource { 

    var dataState: Data? 

    var linkArray = [String]() 
    var titleArray = [String]() 

    @IBOutlet weak var tableView: UITableView! 

    @IBOutlet weak var SearchTextField: UITextField! 

    @IBAction func SearchButton(_ sender: Any) { 

     let urlString = "http://localhost:3000/getimages" 


     Alamofire.request(urlString, method: .post, parameters: ["search": SearchTextField.text!],encoding: JSONEncoding.default, headers: nil).responseJSON { 
      response in 
      switch response.result { 
      case .success: 
       let json = JSON(response.data!) 


       for (_, subJson) in json["data"] { 
        if let link = subJson["link"].string { 
         self.linkArray.append(link) 
        } 
        if let title = subJson["title"].string{ 
         self.titleArray.append(title) 
        } 
       } 
       print("this is linkArray ", self.linkArray) 
       print("this is titleArray ", self.titleArray) 
       self.dataUpdated() 
       break 
      case .failure(let error): 
       print(error) 
      } 
     } 


    } 


    @IBAction func BackToTabControl(_ sender: Any) { 
     let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 

     let tabBarVC = storyBoard.instantiateViewController(withIdentifier: "tabBarController") as! UITabBarController 

     if let vc1 = tabBarVC.viewControllers?.first as? ViewController1 { 
      vc1.dataState = dataState 
     } 

     let appDelegate = UIApplication.shared.delegate as! AppDelegate 
     appDelegate.window?.rootViewController = tabBarVC 
    } 

    func textFieldShouldReturn(_ textField: UITextField) -> Bool { 
     textField.resignFirstResponder() 
     return true 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.tableView.bounces = true 
     SearchTextField.returnKeyType = UIReturnKeyType.done 
     SearchTextField.delegate = self 
     tableView.dataSource = self 

    } 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return titleArray.count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cellReuseIdentifier")! //1. 

     let text = titleArray[indexPath.row] //2. 

     cell.textLabel?.text = text //3. 

     return cell //4. 
    } 

    func dataUpdated() { 
     DispatchQueue.main.async { 
      self.tableView.reloadData() 
     } 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

} 
+0

看起來像(自動)佈局問題。 – vadian

+0

你是否設置了原型單元格的約束條件,先執行然後再運行,當然它的自動佈局問題 –

+0

我不是一個非常聰明的人。我將如何做到這一點?在我看到的約束菜單中(http://imgur.com/a/GDsub)我沒有看到設置該約束的地方(即原型單元格)。 –

回答

0

嘗試下一個:

if let label = cell.textLabel { 
     label.text = text 
     label.sizeToFit() 
    } 
0

你必須點擊和實現代碼如下內舉行得到這個在模擬器滾動.....顯然我不知道這一點。謝謝你們的幫助。

相關問題