1

VC code Overlapping text label issue

I am using Collection View for my application. I am facing cell reusability issue. I am taking a separate class for UICollectionViewCell and I have bound the outlets. Please refer the code snippet below:

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)

 }
}

I badly need the help. Please provide me the solution. Thanks in advance.

Joe
  • 8,868
  • 8
  • 37
  • 59
  • add your `CollectionViewCell1` class and check the value of `cell` variable by printing it. – triandicAnt Oct 30 '16 at 03:53
  • Your code not explaining the problem.can you post your collectionViewCell class and state the issue when you run the project... – Joe Oct 30 '16 at 06:24
  • @Joe here's my complete code snippet. I have edited the code in my question. When I run the app each cell has a label which comes from the array "category". When I scroll up and down I can see that the label text gets overlapped onto each other. So can you please help me in solving this problem? –  Nov 02 '16 at 03:41
  • Your code looks alright to me but I am not sure about your other class. From your code you simple passing the URL link to your 2ndVC.r u using webView to load this URL?..your code not explaining your problem.let me know what problem you facing... – Joe Nov 02 '16 at 04:03
  • But from your above code you can't pass data to your 2ndVC.because you haven't assigned the data string like var strUrl = string() on mainVC and 2ndVC... – Joe Nov 02 '16 at 04:09
  • @Joe Yeah the code works fine and its only on the cells in the collectionview screen that the problem arises when I run the app the main screen which has different cells with a text label and image seems to overlap when you scroll through them. The code snippet of the othe VC is –  Nov 02 '16 at 04:46
  • class NewViewController: UIViewController { @IBOutlet weak var website: UIWebView! var strUrl : NSString = "" override func viewDidLoad() { super.viewDidLoad() print(strUrl) let url = NSURL (string: strUrl as String); let requestObj = NSURLRequest(URL: url!); website.loadRequest(requestObj) self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "MSIDT", style: UIBarButtonItemStyle.Plain, target: nil, action: nil) website.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0) } –  Nov 02 '16 at 04:47
  • Can you post this code into your post. So I can get a better look... – Joe Nov 02 '16 at 04:48
  • I think you hav collectionView layout issue.so can post your screenshot of your collectionView o/p.. – Joe Nov 02 '16 at 04:49
  • @Joe I have added the screenshot of the code and my problem. Please have a look. Thank you so much –  Nov 02 '16 at 05:39
  • i just looked your screenshot...look at my answer on this post http://stackoverflow.com/questions/40131349/enforce-collectionview-to-have-only-2-rows/40133928#40133928 let me know what you thing.... – Joe Nov 02 '16 at 06:28
  • its just a layout issue....dont place a ur label top of each other .just reposition your labels..... – Joe Nov 02 '16 at 06:30
  • @Joe Oh thank you so much. I will look at it and try it out and let u know the updates. Thanks a million. U are a life saver –  Nov 02 '16 at 06:36
  • If my other answer helped you to find the solution give me an upvote..good luck.... – Joe Nov 02 '16 at 06:38
  • @Joe I tried changing the label but it doesn't work. I have just one label on top of the image view. What would be the possible problem? this small thing is stopping me to push my app to the app store.Any suggestions –  Nov 07 '16 at 21:07
  • @Joe Any help on this? –  Nov 16 '16 at 18:47
  • Can you post your collectionViewCell class.did you try autoConstrains your label? – Joe Nov 16 '16 at 19:46
  • @Joe can you share any other contact information of yours so that its easy for me to share my code and communicate. I cant do much here as i am new and don't have much options to chat –  Nov 22 '16 at 20:02
  • I recommend you to repost your question.so some one will come up with a better solution. – Joe Nov 22 '16 at 20:46

1 Answers1

1

Try this in CollectionViewCell1 class

override func prepareForReuse() {
    titlelabel.text = nil
}
  • Thank you so much. but I have already used this override func prepareForReuse() -> Void { imageopenview.image = nil titlelabel.text=nil } –  Dec 09 '16 at 06:11
  • No it doesn't. Plz help –  Jan 04 '17 at 22:17