gpt4 book ai didi

ios - 如何访问 UICollectionView 中选定单元格的索引

转载 作者:搜寻专家 更新时间:2023-11-01 05:53:40 24 4
gpt4 key购买 nike

我的 UICollectionView 中有几个单元格,我在其中创建按钮:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)
let button = UIButton(frame: CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height))
button.setTitle("ButtonTitle")
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
return cell
}

然后我向按钮添加一个 Action ,该按钮切换到一个新的 ViewController,将按钮的名称设置为标题:

func buttonAction(sender: AnyObject?) {
let vc = NewViewController()
vc.title = sender!.titleLabel!!.text
revealViewController().pushFrontViewController(vc, animated: true)
}

但我现在还想访问所选单元格/按钮的索引。有人知道怎么做吗?

最佳答案

喜欢

第一步

 button.tag = indexPath.row
button.setTitle("ButtonTitle")
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

第二步

func buttonAction(sender: UIButton) {

if let sendertitle = sender.titleLabel?.text {

let vc = self.storyboard?.instantiateViewControllerWithIdentifier("NewViewControllerIdentifier") as? NewViewController
vc.title = sendertitle

revealViewController().pushFrontViewController(vc, animated: true)
}
}

选择 -- 2

如果你想实现简单的方法,就喜欢

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)

return cell
}

在 didSelect 调用上

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

let vc = self.storyboard?.instantiateViewControllerWithIdentifier("NewViewControllerIdentifier") as? NewViewController
vc.indexPath = indexPath.row

revealViewController().pushFrontViewController(vc, animated: true)
}

关于ios - 如何访问 UICollectionView 中选定单元格的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35341194/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com