Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 获取indexPath的didSelectItemAt上的值,并将其添加到委托/协议中以填充头单元格_Ios_Swift_Uicollectionview_Delegates_Protocols - Fatal编程技术网

Ios 获取indexPath的didSelectItemAt上的值,并将其添加到委托/协议中以填充头单元格

Ios 获取indexPath的didSelectItemAt上的值,并将其添加到委托/协议中以填充头单元格,ios,swift,uicollectionview,delegates,protocols,Ios,Swift,Uicollectionview,Delegates,Protocols,使用协议/委托&从didSelectItemAt(collectionview)检索数据 宾果游戏正在打印我需要的值。很好 现在,我不能在该函数中使用协议的方法,这是错误的执行,因此编译器或Xcode会说,嘿,您将声明该方法为常规方法,而不是嵌套方法 //Bridge protocol xDelegate { func x(for headerCell: HeaderCell) } 任何想在这方面指导我的人都可以。我想你可以在didSelectItemAt indexPath中将此添加

使用协议/委托&从didSelectItemAt(collectionview)检索数据

宾果游戏正在打印我需要的值。很好

现在,我不能在该函数中使用协议的方法,这是错误的执行,因此编译器或Xcode会说,嘿,您将声明该方法为常规方法,而不是嵌套方法

//Bridge
protocol xDelegate {
    func x(for headerCell: HeaderCell)
}

任何想在这方面指导我的人都可以。我想你可以在
didSelectItemAt indexPath
中将此添加到当前代码的末尾:

guard let header = collectionView.supplementaryView(forElementKind: "yourElementKind", at: indexPath) as? HeaderCell else { return }

header.lbl.text = bingo

collectionView.reloadData()
编辑:暂时保留所有其他内容,以确保首先获得好结果


让我知道这是否有效,很高兴回来查看。

不使用代理,但它会起作用

解决者:

1) 转到collectionView的控制器。创建一个新变量来存储项目

// Pass the item object into the xController so that it can be accessed later. 
//(didSelectItemAt)
 xController.newVar = item 

//xController class: UICollectionView... 
// MARK: - Properties 
var newVar: Thing? 
最后,在该类中,您应该有方法ViewforSupplmentalElementofKind,在该方法中注册HeaderCell,然后只添加

let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerId, for: indexPath) as! HeaderCell

header.lbl.text = newVar.whatEverYourDataIs


几乎没有通用性,但它可以工作。:)

不鼓励您在
didSelectRow
中操作单元格。用户下次滚动时,该值将被还原。更新模型并重新加载行或节。谢谢,您的意思是我需要将数据传递给该单元格的父单元格,该单元格将是单击的项目。。。但我遇到了一个问题,因为我只是添加了一个collectionView作为视图,而不是通过任何导航栏或模式我不是用IBFirst,而是通过编程来完成这项工作的,我脑子里出了个屁,现在意识到你想更新的是标题,而不是你正在点击的单元格。还有比我想象的更多的事情正在发生,所以我现在相应地更新我的帖子。如果它仍然不起作用,请随时PM我或链接git回购。同样的行为,它不会打开新屏幕。感谢您的更新,使用断点header.lbl.text=bingo的cellForRowAt没有以前那么成功了,您有什么想法吗?forElementKind的占位符是:UICollectionView.elementKindSectionHeaderHmm我现在很困惑,想看看你的项目,知道还有什么正在进行和期待的!将代码添加到didSelectItemAt的末尾将打开新窗口,但不会点击header.lbl.text=bingo。不幸的是,我需要查看代码以进一步提供帮助。否则祝你好运!太棒了,非常感谢你,祝你有一个美好的一天(:
guard let header = collectionView.supplementaryView(forElementKind: "yourElementKind", at: indexPath) as? HeaderCell else { return }

header.lbl.text = bingo

collectionView.reloadData()
// Pass the item object into the xController so that it can be accessed later. 
//(didSelectItemAt)
 xController.newVar = item 

//xController class: UICollectionView... 
// MARK: - Properties 
var newVar: Thing? 
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerId, for: indexPath) as! HeaderCell

header.lbl.text = newVar.whatEverYourDataIs