-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
}
是否有異步處理呢?
你應該把你的代碼粘貼到帖子裏面而不是鏈接它。你到目前爲止還嘗試過什麼? – Aggressor 2015-02-09 22:06:38
編輯它。我試圖創建2個新的數組,名爲isUpvoted和isDownvoted,併爲每個項目存儲true和false。然後在cellForRowAtIndexPath中:它會檢查它是否爲真。如果它是真的,它會將背景設置爲藍色,並且按鈕將被禁用。 (上下投票)。我仍然有同樣的問題。 – Young 2015-02-09 22:15:22
所以它實際上每次向上或向下滾動都會創建新的單元格(您可以通過檢查內存地址自行驗證)。你需要做的是存儲你想要的每個單元格的所有屬性,並在這個方法中再次分配它們。 – Aggressor 2015-02-09 22:18:27