2015-11-14 89 views
0

我有一個應用程序,它具有嵌入ViewController中的tableview,每當我導航到另一個ViewController並導航回表格視圖時,單元格在我滾動時重複。有沒有人有任何建議如何防止這種情況?爲實現代碼如下目前的代碼是:自定義單元格重複

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let singleCell: marathonTableViewCell = tableView.dequeueReusableCellWithIdentifier("marathonCell") as! marathonTableViewCell 

    singleCell.marathonName.text = marathonRaces[indexPath.row] 
    singleCell.entry.text = "\(entryNumber[indexPath.row])" 
    singleCell.entries.text = "\(entires[indexPath.row])" 
    singleCell.length.text = "\(length[indexPath.row])" 

    return singleCell 
} 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    let indexPath = self.marathonsTableView.indexPathForSelectedRow! 

    let currentCell = marathonsTableView.cellForRowAtIndexPath(indexPath) as! marathonTableViewCell 

    let marathonEvents = currentCell.marathonName.text 

    self.performSegueWithIdentifier("marathonDetail", sender: self) 
} 

我使用的快捷,xcode7,並解析爲我的後端

內viewDidAppear唯一相關的代碼如下:

var query = PFQuery(className: 「Marathons") 
    query.orderByAscending("end") 
    query.findObjectsInBackgroundWithBlock { (marathons, error:NSError?) -> Void in 
     if(error == nil){ 
     //success 

      for marathon in marathons! { 
      self.marathonRaces 
      .append(marathon[「marathonName"] as! String) 
      self.entry.append(marathon[「entryNumber"] as! Int) 
      self.entries.append(marathon[「entries"] as! Int) 
      self.length.append(marathon[「length"] as! Int) 



      } 

      self.marathonsTableView.reloadData() 

     }else { 
     print(error) 

     } 
    } 
+1

「重複」是什麼意思在「表格視圖單元格重複當我滾動」? – zaph

+0

'marathonRaces'數組有沒有重複元素? – Deva

+0

如果你的數組有3個項目,你將使用3個單元格,每個單元格有4個標籤!和3個標籤在所有單元格上都有相同的文字!這就是你所說的重複? –

回答

1

問題出在您的viewDidAppear方法中。每當控制器出現時,您都從後臺獲取數據,將它們追加到您的數組並重新加載tableview。例如,將用於提取數據的代碼移動到viewDidLoad,並且「重複」應該消失。

+0

謝謝你,我花了幾個小時谷歌搜索和其他嘗試事 – Ace

0

你檢查查看數據源marathonRaces是否獲得更多條目?

您可能會在每個後退導航中添加更多條目,如果是這樣,請在添加條目之前添加或刪除所有條目。

+0

我將如何檢查是否有更多的條目被添加,我不相信它們被添加爲marathonRaces是一個數組,其中數據被附加到分析查詢 – Ace

+0

VAR marathonRaces = [字符串]() VAR項= [INT]() VAR項= [INT]() VAR長度= [INT]() – Ace