2017-07-27 62 views
0

我想填充我的UITableView與我從GET請求收到的一些數據。我不知道爲什麼,但如果在我的numberOfRowsInSection中,我返回一個特定值(例如8)它代表信息。若y回到我的數組被要求填寫的值,則UITableView不添加任何行:TableView不顯示數據

class DashboardTableViewController: UITableViewController { 

    var userData = [String]() 
    var secondUserData = [String]() 
    var name: String = "" 

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

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 
    } 

    /* This function gets the user account information from a JSON in a simple request */ 
    private func loadUserData(completion: @escaping (_ myArray: [String]) -> Void) { 
     let session = Twitter.sharedInstance().sessionStore.session() 
     let client = TWTRAPIClient.withCurrentUser() 
     let userInfoURL = "https://api.twitter.com/1.1/users/show.json" 
     let params = ["user_id": session?.userID] 
     var clientError : NSError? 
     let request = client.urlRequest(withMethod: "GET", url: userInfoURL, parameters: params, error: &clientError) 

     client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in 
      if connectionError != nil { 
       print("Error: \(connectionError)") 
      } 

      do { 
       let json = JSON(data: data!) 
       if let userName = json["name"].string, 
        let description = json["description"].string, 
        let followersCount = json["followers_count"].int, 
        let favouritesCount = json["favourites_count"].int, 
        let followingCount = json["friends_count"].int, 
        let lang = json["lang"].string, 
        let nickname = json["screen_name"].string { 
         self.userData.append(userName) 
         self.userData.append(nickname) 
         self.userData.append(String(followersCount)) 
         self.userData.append(String(followingCount)) 
         self.userData.append(String("22")) 
         self.userData.append(lang) 
         self.userData.append(description) 
         self.userData.append("No country") 

         completion(self.userData) 
       } 
      } 
     } 
    } 

    /* This closure helps us to fill the labels once the request has been finished in loadUserData */ 
    func manageUserData(label: UILabel, index: Int) { 
     loadUserData { 
      (result: [String]) in 
      label.text = result[index] 
     } 
    } 

    // MARK: - Table view data source 
    override func numberOfSections(in tableView: UITableView) -> Int { 
     // #warning Incomplete implementation, return the number of sections 
     return 1 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     // #warning Incomplete implementation, return the number of rows 
     return self.userData.count 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let rowNumber = indexPath.row 
     let cellIdentifier = "TableViewCell" 
     guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TableViewCellController else { 
      fatalError("The dequeued cell is not an instance of TableViewCellController.") 
     } 

     switch rowNumber { 
     case 0: 
      cell.titlePlaceholder.text = "Name:" 
      manageUserData(label: cell.valuePlaceholder, index: rowNumber) 
     case 1: 
      cell.titlePlaceholder.text = "Nickname:" 
      manageUserData(label: cell.valuePlaceholder, index: rowNumber) 
     case 2: 
      cell.titlePlaceholder.text = "Followers:" 
      manageUserData(label: cell.valuePlaceholder, index: rowNumber) 
     case 3: 
      cell.titlePlaceholder.text = "Following:" 
      manageUserData(label: cell.valuePlaceholder, index: rowNumber) 
     case 4: 
      cell.titlePlaceholder.text = "Tweets:" 
      manageUserData(label: cell.valuePlaceholder, index: rowNumber) 
     case 5: 
      cell.titlePlaceholder.text = "Language:" 
      manageUserData(label: cell.valuePlaceholder, index: rowNumber) 
     case 6: 
      cell.titlePlaceholder.text = "Biography:" 
      manageUserData(label: cell.valuePlaceholder, index: rowNumber) 
     case 7: 
      cell.titlePlaceholder.text = "Country:" 
      manageUserData(label: cell.valuePlaceholder, index: rowNumber) 
     default: 
      cell.titlePlaceholder.text = "?" 
      cell.valuePlaceholder.text = "?" 
     } 

     return cell 
    } 
} 

任何想法,爲什麼發生這種情況? 非常感謝!

更新!

我解決了這個問題,在loadUserData()函數內的completion()之後添加self.tableView.reloadData()。希望能幫助到你!

+0

添加覆蓋func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)開始處的斷點 - > UITableViewCell {' 檢查此方法是否被稱爲 – Maxime

+0

這是關於您的請求。嘗試在'let json = JSON(data:data!)'後面打印你的json,看看會發生什麼 – GIJOW

+0

它只有在我在override func tableView中返回一個特定值時纔會執行這兩個操作(_ tableView:UITableView,numberOfRowsInSection section:Int ) - > Int –

回答

2

斯威夫特解請求後,必須重新加載表視圖數據:

tableView.reloadData() 

如果不從主線程調用它:

DispatchQueue.main.async { 
    self.tableView.reloadData() 
} 
+0

已嘗試使用self.tableView.realoadData(),它仍然不會加載它如果我在numberOfRowsInSection函數中返回了我的數組的數量 –

+1

代碼中的哪裏重載了tableView? – Phyber

+0

我已經試過在完成後在做請求的方法中執行此操作 –

0

接收與

[self.tableView reloadData]; 
+0

已經嘗試使用self.tableView.realoadData(),它仍然不加載它,如果我在numberOfRowsInSection函數返回我的數組的計數 –