2016-10-30 67 views
0

VC code Overlapping text label issueUICollectionView細胞的可重用性問題 - 迅速

我使用收藏查看我的應用程序。我面臨着細胞重用問題。我正在爲UICollectionViewCell創建一個單獨的類,並且我已經綁定了這些網點。請參閱下面的代碼片段:

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) 

} 
} 

我非常需要幫助。請爲我提供解決方案。提前致謝。

+0

添加'CollectionViewCell1'類,並通過打印它檢查cell'變量'值。 – triandicAnt

+0

您的代碼沒有解釋問題。您可以發佈您的collectionViewCell類,並在運行該項目時聲明問題... – Joe

+0

@Joe這裏是我的完整代碼片段。我編輯了我的問題中的代碼。當我運行應用程序時,每個單元格都有一個來自數組「類別」的標籤。當我上下滾動時,我可以看到標籤文本互相重疊。那麼你能幫我解決這個問題嗎? – Nilesh

回答

0

試試這個在CollectionViewCell1

override func prepareForReuse() { 
    titlelabel.text = nil 
} 
+0

非常感謝。但我已經用這個 覆蓋func prepareForReuse() - >無效{ imageopenview.image =無 titlelabel.text =無 } – Nilesh

+0

@Nilesh - 它適合你? –

+0

不,它不。 Plz的幫助 – Nilesh