2015-02-09 100 views
-1

我一直在使用swift和parse在線學習yikyak克隆的教程。我正在使用coreData存儲upvoted/downvoted項目的objectID。當tableview單元格被加載時,它會檢查parse中的objectID是否在coreData中,並通過將背景圖像添加到特定按鈕並禁用向上和向下按鈕來響應。不過,我面臨的一個問題是,上下滾動幾次會導致隨機單元格具有背景並禁用其按鈕。Swift:Table Cells沒有正確設置按鈕

這裏是代碼(:)的cellForRowAtIndexPath鏈接:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as PullTableViewCell 
    let restaurant = self.restaurantData[indexPath.row] 
    cell.scoreLabel.text = "\(score)" 

    cell.plusButton.tag = indexPath.row; 
    cell.plusButton.addTarget(self, action: "plusOne:", forControlEvents: .TouchUpInside) 
    cell.minusButton.tag = indexPath.row 
    cell.minusButton.addTarget(self, action: "minusOne:", forControlEvents: .TouchUpInside) 

    if (cell.plusButton.enabled) { 
     // upvotes 
     let request = NSFetchRequest(entityName: "Upvotes") 
     let moc:NSManagedObjectContext = self.managedObjectContext! 

     var error: NSErrorPointer = nil 
     self.upvoteData = moc.executeFetchRequest(request, error: error) as [Upvotes] 

     for (var i = 0; i < self.upvoteData.count; i++) { 
      if (self.upvoteData[i].objectid == self.objectIDs[indexPath.row]) { 
       NSLog("the cell is in view is \(indexPath.row)") 
       cell.plusButton.backgroundColor = UIColor.blueColor() 
       cell.minusButton.enabled = false 
       cell.plusButton.enabled = false 
      } 
     } 

     // downvotes 
     let request2 = NSFetchRequest(entityName: "Downvotes") 
     let moc2:NSManagedObjectContext = self.managedObjectContext! 

     var error2: NSErrorPointer = nil 
     self.downvoteData = moc2.executeFetchRequest(request2, error: error2) as [Downvotes] 

     for (var i = 0; i < self.downvoteData.count; i++) { 
      if (self.downvoteData[i].objectid == self.objectIDs[indexPath.row]) { 
       cell.minusButton.backgroundColor = UIColor.blueColor() 
       cell.minusButton.enabled = false 
       cell.plusButton.enabled = false 
      } 
     } 

    } 
    return cell 
} 

是否有異步處理呢?

+0

你應該把你的代碼粘貼到帖子裏面而不是鏈接它。你到目前爲止還嘗試過什麼? – Aggressor 2015-02-09 22:06:38

+0

編輯它。我試圖創建2個新的數組,名爲isUpvoted和isDownvoted,併爲每個項目存儲true和false。然後在cellForRowAtIndexPath中:它會檢查它是否爲真。如果它是真的,它會將背景設置爲藍色,並且按鈕將被禁用。 (上下投票)。我仍然有同樣的問題。 – Young 2015-02-09 22:15:22

+0

所以它實際上每次向上或向下滾動都會創建新的單元格(您可以通過檢查內存地址自行驗證)。你需要做的是存儲你想要的每個單元格的所有屬性,並在這個方法中再次分配它們。 – Aggressor 2015-02-09 22:18:27

回答

0

當你上下滾動時,tableview實際上會重新創建單元格。所以當一個單元格滾動出視圖時,它可能會被破壞並重新創建。

您需要在地圖內部存儲單元格屬性,然後每次重新初始化單元格。

下面是我自己的代碼示例:

public func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell 
    { 
     var cell = collectionView.dequeueReusableCellWithReuseIdentifier("selectPhotoCell", forIndexPath: indexPath) as B_SelectPhotoControllerViewCell 
     cell.indexPath = indexPath 
     let asset = currentAssetAtIndex(indexPath.item) 
     PHImageManager.defaultManager().requestImageForAsset(asset, targetSize:_cellSize, contentMode: .AspectFit, options: nil) 
     { 
      result, info in 
      if(cell.indexPath == indexPath) 
      { 
       cell.imageView.image = result 
       cell.imageView.frame = CGRectMake(0, 0,self._cellSize.width,self._cellSize.height) 
       for editImage in self._editImageChicklets 
       { 
        if(editImage.selectedPhotoIndexPath != nil && editImage.selectedPhotoIndexPath == indexPath) 
        { 
         cell.number = editImage.selectedPhotoDisplayNumber! 
        } 
       } 
      } 
     } 
     return cell 
    } 

這是一個使用照片圖像從用戶照片輥上的UICollectionView的例子。我跟蹤indexPath,如果匹配,那麼我將圖像分配給屬性。

在你的情況下,你需要做同樣的事情。保留單元格屬性和索引路徑的地圖:

Dictionary<NSIndexPath,CustomCellPropertiesObject> 

然後重新進行適當的初始化。