我需要帮助将变量放在 UICollectionView 单元格上,并稍后通过 ViewController.swift 中的函数获取 我尝试了这段代码,但在 cell.idyoutube.text 中获取变量时遇到问题
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if (collectionView == self.collectionView1)
{
var cell : FeaturedCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifierFeatured, forIndexPath: indexPath) as! FeaturedCollectionViewCell
var imageFilename: UIImage!
let imgURL: NSURL = NSURL(string: "http://www.mywebsite/images/imagestvos/feature-\(indexPath.row).jpg")!
let request: NSURLRequest = NSURLRequest(URL: imgURL)
imageFilename = UIImage(data: data!)!
cell.featuredImage.image = UIImage(data: data!)!
cell.idyoutube.text = "Rqr3w8scm2E"
}
task.resume()
return cell
}
return UICollectionViewCell()
}
这里,这是通过点击激活的功能,但在获取 idyoutube.tex
变量时遇到问题。
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let cell : FeaturedCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifierFeatured, forIndexPath: indexPath) as! FeaturedCollectionViewCell
let mylink = cell.idyoutube.text
}
我不知道是否到达idyoutube我去:let cell :FeaturedCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifierFeatured, forIndexPath: indexPath) as!精选CollectionViewCell
最佳答案
您不应该在 -didSelectItemAtIndexPath
调用中使用 -dequeueReusableCellWithReuseIdentifier
方法,因为此调用实际上是尝试从单元池中获取空闲单元,而不是代表该单元您正在点击。
您需要使用索引路径调用 -[UICollectionView cellForItemAtIndexPath:]
,并将其传递给 -didSelectItemAtIndexPath:
方法。
在返回的单元格中,您将找到存储的 idyoutube.text
属性。
关于ios - 如何将变量放在 UICollectionView 上并通过函数获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35127932/