UICollectionView 单元可重用性问题 - swift

Posted

技术标签:

【中文标题】UICollectionView 单元可重用性问题 - swift【英文标题】:UICollectionView cell reusability issue - swift 【发布时间】:2016-10-30 02:53:46 【问题描述】:

VC code Overlapping text label issue

我正在为我的应用程序使用集合视图。我正面临细胞可重用性问题。我正在为 UICollectionViewCell 上一个单独的课程,并且我已经绑定了网点。请参考下面的代码sn-p:

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource 

@IBOutlet weak var collectionview: UICollectionView!

// An array to create desired number of cells in the home screen collection view 

let category = ["About Us", "Program Structure", "Courses", "Linkedin" , "Latest News", "Contact Us","Facebook", "Twitter", "Youtube", "Student Portal", "Alumni", "Faculty", "Career Center", "Free Software", "Apply Now", "FAQ"]

let imagearray = [UIImage (named: "Entypo_d83d(1)_512"), UIImage (named: "Entypo_d83d(0)_1024-6"), UIImage (named: "Entypo_d83d(0)_1024"), UIImage (named: "Entypo_f318(0)_1024"), UIImage (named: "Entypo_e776(0)_1024"), UIImage (named: "Entypo_d83d(0)_1024-2"),UIImage (named: "Entypo_f30c(0)_1024"), UIImage (named: "Entypo_f309(0)_1024"), UIImage (named: "Entypo_d83c(0)_1024-1"), UIImage (named: "Entypo_e78e(0)_1024"), UIImage (named: "Entypo_e722(0)_1024"), UIImage (named: "Entypo_d83d(0)_1024-1"), UIImage (named: "Entypo_d83d(0)_1024-3"), UIImage (named: "Entypo_d83d(0)_1024-4"), UIImage (named: "Entypo_e789(0)_1024"), UIImage (named: "Entypo_d83d(0)_1024-5")]

let webarray = ["http://ed.fullerton.edu/msidt/about-our-department/", "http://ed.fullerton.edu/msidt/future-students/program-structure/", "http://ed.fullerton.edu/msidt/future-students/courses/", "https://www.linkedin.com/in/msidt-csuf-9ab982119", "http://ed.fullerton.edu/msidt/category/news/", "http://ed.fullerton.edu/msidt/about-our-department/contact-information/", "https://www.facebook.com/MSIDTFullerton/", "https://twitter.com/msidt", "https://www.youtube.com/watch?v=hAu_Ef22gvA", "https://shibboleth.fullerton.edu/idp/profile/SAML2/Redirect/SSO?execution=e1s1", "http://ed.fullerton.edu/msidt/alumni/", "http://ed.fullerton.edu/msidt/faculty/", "http://www.fullerton.edu/career/", "http://www.fullerton.edu/it/students/software/", "http://ed.fullerton.edu/msidt/apply-now/", "http://ed.fullerton.edu/msidt/future-students/faq/"]

override func viewDidLoad() 
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.     


//to set the number of cells

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
    return self.category.count


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

    let cell = collectionview.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell1

    cell.imageopenview.image = self.imagearray[indexPath.item]

    cell.titlelabel .text = self.category[indexPath.item]

    return cell   


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


    let secondViewController = self.storyboard!.instantiateViewControllerWithIdentifier("newVC") as! NewViewController
    secondViewController.strUrl = webarray[indexPath.item]
    self.navigationController!.pushViewController(secondViewController, animated: true)

 

我非常需要帮助。请给我解决方案。提前致谢。

【问题讨论】:

添加您的CollectionViewCell1 类并通过打印检查cell 变量的值。 您的代码没有解释问题。您能否发布您的 collectionViewCell 类并在运行项目时说明问题... @Joe 这是我的完整代码 sn-p。我已经在我的问题中编辑了代码。当我运行应用程序时,每个单元格都有一个来自数组“类别”的标签。当我上下滚动时,我可以看到标签文本彼此重叠。那么你能帮我解决这个问题吗? 您的代码在我看来没问题,但我不确定您的其他课程。从您的代码中,您只需使用 webView 将 URL 链接传递给您的 2ndVC.r 以加载此 URL?...您的代码没有解释您的问题。让我知道您面临什么问题... 但是从您上面的代码中,您无法将数据传递给您的 2ndVC。因为您没有在 mainVC 和 2ndVC 上分配像 var strUrl = string() 这样的数据字符串... 【参考方案1】:

CollectionViewCell1 类中试试这个

override func prepareForReuse() 
    titlelabel.text = nil

【讨论】:

非常感谢。但我已经使用过这个覆盖函数 prepareForReuse() -> Void imageopenview.image = nil titlelabel.text=nil 不,它没有。请帮助

以上是关于UICollectionView 单元可重用性问题 - swift的主要内容,如果未能解决你的问题,请参考以下文章

UICollectionView 上的可重用性问题

UICollectionView 动画可重用单元格?

使用 SDWebImage 为 UICollectionView 可重用单元从路径加载图像 - Swift

uicollectionview 可重用单元格图像在滚动时加载

UICollectionView 不重用单元格

重用 UICollectionView 中的单元格时,会短暂显示重用 UICollectionViewCell 的旧内容