ios - 对 UICollectionViewCell 的选择执行操作?

标签 ios swift uicollectionview uicollectionviewcell

我想在按下单元格时调用一个函数,并且我希望能够根据按下的单元格来更改该函数的功能。这是我目前的代码:

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    //#warning Incomplete method implementation -- Return the number of sections
    return 4
}


override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    //#warning Incomplete method implementation -- Return the number of items in the section
    return 10
}

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

    // Configure the cell

    return cell
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
    return CGSizeMake(90, 90)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets
{
    return UIEdgeInsetsMake(10, 10, 10, 10)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat
{
    return 10
}


// MARK: UICollectionViewDelegate


// Uncomment this method to specify if the specified item should be highlighted during tracking
override func collectionView(collectionView: UICollectionView, shouldHighlightItemAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}



// Uncomment this method to specify if the specified item should be selected
override func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}



// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
override func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

override func collectionView(collectionView: UICollectionView, canPerformAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool {
    return true
}

override func collectionView(collectionView: UICollectionView, performAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {
    self.printThatItWorked()
}

func printThatItWorked()
{
    println("It worked")
}

只要选择一个单元格,就应该调用函数printThatItWorked(),最终我希望能够让它打印出单元格编号x被按下。我该如何去做呢?

最佳答案

委托(delegate)方法 didSelectCell... 是您要放置该代码的位置。

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    // You can use indexPath to get "cell number x", or get the cell like:
    let cell = collectionView.cellForItemAtIndexPath(indexPath)
    printThatItWorked()
}

关于ios - 对 UICollectionViewCell 的选择执行操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30537531/

相关文章:

ios - 删除一个后,如何获取新中心 UICollectionViewCell 的 indexPath?

ios - didSelectItem 未被调用

ios - Itunesconnect App 预览视频

ios - OneSignal iOS SDK 发送通知失败代码 400

ios - 如何在 SFSafariViewController 或 Webview 中打开 Twitterkit

ios - UICollectionView 不重用单元格

ios - UITextField 数字键盘 : dot instead of comma for float values

ios - 从 iOS 上的 OpenGL ES 3.0 读取 RGB8 缓冲区?

ios - 了解一对多的核心数据删除规则

swift - Collection View 单元格上的重叠阴影