2015-12-05 56 views
1

你好,我一直有這個問題一段時間。我想停止重新使用單元格的tableview。當我滾動時,它會一直顯示錯誤的信息,然後顯示幾毫秒的正確信息。我該如何停止重用單元格的tableview,或者如何重用單元格並使其不能這樣做。Tableview重用單元格並首先顯示錯誤的數據

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

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cellIdentifier = "CategoryTableViewCell" 
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CategoryTableViewCell 
    cell.nameLabel.text = cats[indexPath.row].categoryName 
    cell.subNameLabel.text = cats[indexPath.row].appShortDesc 
    let catImageUrl = cats[indexPath.row].imageUrl 
      let url = NSURL(string: "https:\(catImageUrl)") 
      let urlRequest = NSURLRequest(URL: url!) 
      NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue()) { (response, data, error) -> Void in 
       if error != nil { 
        print(error) 
       } else { 
        if let ass = UIImage(data: data!) { 
          cell.photoImageView.image = ass 
         } 
        self.loading.stopAnimating() 
       } 
      } 
    return cell 
} 

回答

8

問題是您正在看到來自先前單元格的圖像。

cell.photoImageView.image = nil 

或將其設置爲你所選擇的默認圖像:當你出隊的重用細胞圖像只需初始化爲nil


請注意,您在加載後更新映像的方式有問題。

  1. 當圖像最終加載時,該行可能不再顯示在屏幕上,因此您將更新已被重新使用的單元格。

  2. 更新應該在主線程上完成。

一個更好的方法來做到這一點將有一個數組緩存單元格的圖像。將圖像加載到陣列中,然後告知tableView重新加載該行。

事情是這樣的:

dispatch_async(dispatch_get_main_queue()) { 
    self.imageCache[row] = ass 
    self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: row, inSection: 0)], 
     withRowAnimation: .None) 
} 
+0

我可以像這樣一千倍。非常感謝,我不能相信我沒有弄清楚自己。再次感謝 – Slygoth

相關問題