我在集合视图单元格中有一个按钮,当按下时我想转到另一个视图控制器并将字符串传递给该视图控制器。我唯一的问题是传递数据,我不知道如何检查按钮被点击的单元格。
http://bucketname.mycompany.com.s3-website-us-west-1.amazonaws.com/spacebackground(1)Full.jpg
答案 0 :(得分:0)
向您的单元格类添加一个字符串变量,并在cellForRow中为其提供所需的strValue
// btn action in cell
@IBAction func btnClicked(_ sender: Any)
{
/// access cell and get it's string var , self = cell
/// here use delegate to push another view controller with self.strValue
}
答案 1 :(得分:0)
试试这个(我假设这里只允许单一选择,而且我假设通过选择一个单元格来启动segues):
if segue.identifier == "Testing1" {
var view = segue.destination as! ViewTheAccount
if let itemIndex = collectionView.indexPathsForSelectedItems?.first?.item {
let selectedItem = self.posts[itemIndex]
// do here what you need
}
}
答案 2 :(得分:0)
所以一种方法是在您正在使用的委托调用或回调中发送单元格。
class SomeViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var collectionView:UICollectionView!
collectionView(method that dequeues the cell){
let yourcell = collection view.dequeue(...) as! SomeCell
yourcell.somecallback = callback
}
func callback(cell: UICollectionViewCell){
//To find out which cell it is just
let indexPath = collection view.indexPathForCell(cell)
//YOU NOW know which cell this was sent from.
}
}
class SomeCell: UICollectionViewCell{
var somecallback:((UICollectionViewCell)->())?
func didPress(sender: UIButton){
somecallback(self)
}
}