Swift:Table Cells没有正确设置按钮

时间:2015-02-09 22:01:01

标签: ios uitableview swift core-data

我一直在线阅读使用swift和parse的yikyak克隆教程。我正在使用coreData存储upvoted / downvoted项的objectID。加载tableview单元格时,它会检查解析时的objectID是否在coreData中,并通过向特定按钮添加背景图像并禁用向上和向下投票按钮来做出相应响应。但是,我面临的问题是,向上和向下滚动几次会导致随机单元格具有背景并禁用其按钮。

以下是代码的链接(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
}

是否与异步处理有关?

1 个答案:

答案 0 :(得分: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>

然后仔细检查并重新初始化。