2017-06-28 58 views
1

我在表格視圖中介紹和修復了x等於1時的數據會被引入到數據中,等於2會被轉移到修復數據中,但是當我在執行後是空白的表視圖,以及我的委託和數據源是有一個環節加載表格視圖數據

這裏是我的下載數據功能:

func laodingTableviewData() { 
    self.suggestionTableView.isHidden = true 
    let frame = CGRect(x: 150, y: 283, width: 80, height: 80) 
    let color = UIColor(red: 50.0/255.0, green: 124.0/255.0, blue: 203.0/255.0, alpha: 1) 
    let activityIndicatorView = NVActivityIndicatorView(frame: frame, type: .ballSpinFadeLoader, color: color, padding: 20) 

    self.view.addSubview(activityIndicatorView) 
    activityIndicatorView.startAnimating() 


    if self.x == 1 { 
     DispatchQueue.main.async { 
      print("Start download suggestion data...") 
      self.suggestions = Suggestion.downloadAllSuggestion() 
      self.suggestionTableView.isHidden = false 
      self.suggestionTableView.reloadData() 
     } 

    } else if self.y == 2 { 
     DispatchQueue.main.async { 
      print("Start download repair data...") 
      self.repairs = Repair.downloadAllRepair() 
      self.suggestionTableView.isHidden = false 
      self.suggestionTableView.reloadData() 
     } 

    } 
     activityIndicatorView.stopAnimating() 
} 

我在self.suggestions = advice.downloadAllSuggestion()設置斷點,我相信一定有被抓住的數據,但無法在表格視圖中顯示,請幫忙解答,謝謝!

下面是表視圖函數

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: "mycell", for: indexPath) as! FirstTableViewCell 

    if x == 1 { 
     cell.suggestions = suggestions[indexPath.row] 
    } else if y == 2 { 
     cell.repairs = repairs[indexPath.row] 
    } 
    return cell 

} 


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    var value = 0 

    switch value { 
    case self.x: 
     value = suggestions.count 
    case self.y: 
     value = repairs.count 
    default: 
     break 
    } 
    return value 
} 
+0

添加代碼的cellForRowAtIndexPath&numberRowsInSection方法 – Subramanian

+1

錯流肯定的,'downloadAllSuggestion()'可能是異步的,這意味着'reloadData()'下載完成之前被調用,導致你的表視圖犯規更新 – Tj3n

+0

@ Tj3n那請問不正確,因爲異步方法不應該有返回類型。我也對'Suggestion.downloadAllSuggestion()'裏面的東西感到困惑' –

回答

2

numberOfRowsInSection,值被初始化爲0。

而傳遞到開關殼體。它會始終默認。所以numberOfRowsInSection總是返回0

你的功能假設是這樣的。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    var value = 0 

    if self.x == 1 { 
     value = suggestions.count 
    } else if self.y == 2 { 
     value = repairs.count 
    } 

    return value 
} 
+0

謝謝你的幫助!原來是這個小錯誤... –

1

根據我的理解,您基於x和y值有條件地綁定您的數據。你的numberOfRowsInSection方法也應該反映相同。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if x == 1 { 
     return suggestions.count 
     } else if y == 2 { 
      return repairs.count 
      } 
    return 0 
    } 

希望它有幫助。快樂編碼!

+0

謝謝你的幫助! –

+0

很高興你的問題解決了! – luckyShubhra

+0

謝謝!你真的很快回答^^ –