iOS Swift - 使用带有可重用标识符单元格的

时间:2016-09-23 17:11:47

标签: swift uicollectionview uilabel uicollectionviewcell

嗯,我的问题是我有一个度假村的这个应用程序,他们告诉我他们更喜欢使用Collection Views来显示项目列表,例如我要在这篇文章中添加的子部分是来自他们称之为“Wellness”的部分,因此,我有一个项目列表,我已经以编程方式添加了文本,数字和临时图像(他们没有明确的图像所以...)关键是我的代码完美运行,但是当我向上和向下滚动几次时,我遇到了一个问题,我发现第一个单元格内的标签显示它根本不符合我的约束。 这是我在内部使用CollectionView的视图控制器的代码。

import UIKit

class WellnessViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var introduction: UILabel!

struct cells {
    let number: Int!
    let text: String!
    let image: UIImage!
}
var structArray = [cells]()
var textArray = ["Our Massage offerings incorporate traditional techniques with local ingredients to create a transformative experience.\n Nekupe Express Massage\n Zen Blend Swedish Massage  Alfredo’s Favorite Therapeutic Massage\n Ometepe Couples Massage","Licensed therapists will share their knowledge of Eastern practices to yield extraordinary results for guests.\n Abhyanga (Ayurvedic) Balancing Massage\n Tok Sen Ancient Healing Practice\n Dry Needling with Cranial Sacral Therapy","A treat for the skin, Nekupe offers cleansing and rejuvenating facials for adult women and men that will leave your skin radiant.\n Express 30-minute Facial\n Nekupe Deep Cleansing\n Zen Relaxing Facial \nThe Magnum Facial for Men","Choco Love Cacao Body Masque\n Mombacho Magnifico Salt and Sand Body Masque\n Sweet Tropical Wellness Pineapple Body Masque\n Café Con Amor Coffee Body Treatment\n Super Hydrating Coconut Body Treatment","Nekupe has teamed up with Zen Yoga Nicaragua, a studio located San Juan del Sur, to offer a daily Vinyasa Flow Yoga class on Mirador Deck as well Restorative Yoga, Ashtanga, Yin Yoga, and Power Yoga classes. Additionally, Nekupe offers an annual Yoga Retreat hat features vegetarian meals, spa treatments and one-on-one sessions.","Therapeutic bodywork consultation with Dr. Adam Friedman that focuses on:\n Transformational  Coaching\n Metabolic Conditioning\n Naturopathic Lifestyle and Nutrition Program","Nekupe offers opportunities around the entire reserve for guests to exercise, engage and challenge themselves through physical activity, from cardio and weights to personal training, fitness paths and swimming.\n\nIt features a full-service gym, with a personal trainer (upon request), two professional tennis courts, and mountain biking trails through our diverse landscape. All of the activities are designed for guests with different skills and fitness levels, guaranteeing a good exercise for everyone. Three pools are available for our guests, two of them located at La Residencia Doña Theresita, another one located at Casa Club."]
var imageArray = [UIImage(named: "2.jpg"),UIImage(named: "3.jpg"),UIImage(named: "4.jpg"),UIImage(named: "5.jpg"),UIImage(named: "6.jpg"),UIImage(named: "7.jpg"),UIImage(named: "1.jpg")]
override func viewDidLoad() {
    super.viewDidLoad()
    introduction.text = "OASIS’ central spa location offers nourishing and restorative treatments in our main spa, with three treatment rooms and a zen garden. Satellite treatment spots are located throughout the resort."
    introduction.sizeToFit()
    structArray = [cells(number: 1, text: "Oasis Massage",image: UIImage(named: "2.jpg") ),cells(number: 2, text: "Oasis Specialized Treatments",image: UIImage(named: "3.jpg")),cells(number: 3, text: "Oasis Facials",image: UIImage(named: "4.jpg")),cells(number: 4, text: "Oasis Masques & Body Treatments", image: UIImage(named: "5.jpg")),cells(number: 5, text: "Nekupe Yoga", image: UIImage(named: "6.jpg")),cells(number: 6, text: "Life Coaching", image: UIImage(named: "7.jpg")),cells(number: 7, text: "Nekupe Fitness Center", image: UIImage(named: "1.jpg"))]
    collectionView.backgroundColor=UIColor.clearColor()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.structArray.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! WellnessViewCell
    cell.text.text=structArray[indexPath.row].text
    cell.image.image=structArray[indexPath.row].image
    cell.text.sizeToFit()
    return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    self.performSegueWithIdentifier("DetailWellness", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "DetailWellness" {
        let indexPaths = self.collectionView!.indexPathsForSelectedItems()!
        let indexPath = indexPaths[0] as NSIndexPath

        let vc = segue.destinationViewController as! DetailWellnessViewController
        vc.title = self.structArray[indexPath.row].text
        vc.image = self.imageArray[indexPath.row]!
        //vc.text = self.textArray[indexPath.row]
    }
}

请忽略prepareForSegue,这是一种将信息传递给我接下来的单个视图控制器的方法,这在此问题中并不重要。 这将是我的CollectionView Cell

class WellnessViewCell: UICollectionViewCell {

    @IBOutlet weak var image: UIImageView!

    @IBOutlet weak var text: UILabel!

    override func prepareForReuse() {
        image.image=nil
        text.text=nil
    } 
}

嗯,这就是我拥有的所有代码,我的集合视图单元格中的方法“prepareForReuse()”人们告诉我,也许这是因为每次我们滚动回到顶部的集合视图,他们再一次做代表,所以也许这就是我的问题,但它不起作用,现在我将在模拟器中向您展示它是如何工作的。

This is how it looks once I open the Wellness Tab

And this is how it looks when I scroll down and I go back to top

任何人都知道这个问题的解决方案,任何帮助都会很棒!

0 个答案:

没有答案