如何将变量放在 UICollectionView 上并通过函数获取
How to put variable on UICollectionView and get by function
我需要帮助将变量放在 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! FeaturedCollectionViewCell
你不应该在 -didSelectItemAtIndexPath
调用中使用 -dequeueReusableCellWithReuseIdentifier
方法,因为这个调用实际上是试图从单元池中获取空闲单元而不代表你正在点击的单元。
您需要 -[UICollectionView cellForItemAtIndexPath:]
使用索引路径调用,传递给 -didSelectItemAtIndexPath:
方法。
在返回的单元格中,您会找到存储的 idyoutube.text
属性.
我需要帮助将变量放在 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! FeaturedCollectionViewCell
你不应该在 -didSelectItemAtIndexPath
调用中使用 -dequeueReusableCellWithReuseIdentifier
方法,因为这个调用实际上是试图从单元池中获取空闲单元而不代表你正在点击的单元。
您需要 -[UICollectionView cellForItemAtIndexPath:]
使用索引路径调用,传递给 -didSelectItemAtIndexPath:
方法。
在返回的单元格中,您会找到存储的 idyoutube.text
属性.