如何在UICollectionView中的位置删除或调暗单元格

时间:2015-07-16 19:40:53

标签: ios swift uicollectionview

我正在尝试从特定索引位置的集合视图中删除或调暗一个单元格,但我似乎无法找到此文档。我该如何处理?我试图在updateScore方法中执行此操作。感谢。

class ViewController2: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

var tableImages: [String] = ["image1.png", "image2.png", "image3.png", "image4.png", "image5.png", "image6.png", "image7.png", "image8.png", "image9.png", "image10.png", "image11.png", "image12.png", "image13.png", "image14.png", "image15.png", "image16.png", "image17.png", "image18.png", "image19.png", "image20.png", "image21.png", "image22.png", "image23.png", "image24.png", "image25.png", "image26.png", "image27.png", "image28.png", "image29.png", "image30.png"]

var location:Int = 0


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return tableImages.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: colvwCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! colvwCell


    cell.imgCell.image = UIImage(named: tableImages[indexPath.row])
    return cell

}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    print("Cell \(indexPath.row) selected")

    location = indexPath.row

    self.performSegueWithIdentifier("popUpSegue", sender: nil)

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if (segue.identifier == "popUpSegue") {
        let svc = segue.destinationViewController as! ViewController3
        svc.location = location

    }
}

static func updateScore(location: Int){
    print(location)

    // remove cell at location

}

static func updateScore2(location: Int){
    print(location)

    // dim cell at location

}

2 个答案:

答案 0 :(得分:1)

您可以使用tableView:deleteItemsAtIndexPaths移除index的单元格,但您需要考虑以下列方式更新dataSource

static func updateScore(location: Int){

    // create the indexPath
    var indexPath = NSIndexPath(forRow: location, inSection: 0)

    self.tableView.beginUpdates()

    // delete the row at indexPath , you can hanlde the animation you want too
    self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .None)

    // update your dataSource size
    self.tableImages.removeAtIndex(location)

    self.tableView.endUpdates()
}

每次添加/删除表项时,都会调用tableView:numberOfRowsInSection:方法 - 除非您使用begin / endUpdate包围这些调用。如果您的数组和表视图项不同步,则在没有开始/结束调用的情况下将抛出异常。

我希望这对你有所帮助。

答案 1 :(得分:0)

首先,您必须将其从tableImages数组中删除:

tableImages.removeAtIndex(location)

然后你必须从集合视图中删除它:

let indexPath = NSIndexPath(forRow: location, inSection: 0)
collectionView.deleteItemsAtIndexPaths([indexPath])

请记住 - 您的后备存储(tableImages)和集合视图必须保持同步。