创建UICollectionView时出现问题,多次创建资源

时间:2017-02-08 16:49:38

标签: ios swift swift3 uicollectionview ios10

我已在主视图中成功创建了多个集合视图,在我的单元格内以编程方式创建标签,但每次集合视图刷新时,它都会创建与预览标签重叠的新标签。如何删除以前的数据并创建新资源而不覆盖?

Image 1 -> error displaying collectionview

这是我的代码。

CollectionViews的插座

@IBOutlet var collection1: UICollectionView!
@IBOutlet var collection2: UICollectionView!
@IBOutlet var collection3: UICollectionView!
@IBOutlet var collection4: UICollectionView!

代表收集

collection1.dataSource = self
    collection1.delegate = self
    collection1.register(UICollectionViewCell.self,   forCellWithReuseIdentifier: "Cell1")
    collection1.backgroundColor = collectionBG
    self.view.addSubview(collection1)

集合视图函数

 // MARK: - COLLECTIONVIEW FUNC
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    var count: Int?
    if collectionView == diasSemana{
        count =  dias.count
    }
    if collectionView == collection1{
        count =  instructores.count
    }
    if collectionView == collection2{
        count = instructores.count
    }
    if collectionView == collection3{
        count = instructores.count
    }
    if collectionView == collection4{
        count = instructores.count
    }
    return count!
}

// MARK: - COLLECTIONVIEW FUNC
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    var cell: UICollectionViewCell?

    if collectionView == diasSemana{

        cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as UICollectionViewCell


        let title = UILabel(frame: CGRect(x: CGFloat(15), y: CGFloat(0), width: CGFloat((cell?.bounds.size.width)!), height: CGFloat(30)))
        //title.addTextSpacing(1.5)
        title.font = UIFont(name:"GothamRounded-Book", size: 11)

        title.tag = 16
        title.text = dias[indexPath.row]
        cell?.contentView.addSubview(title)


    }
    if collectionView == collection1{

        cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell1", for: indexPath) as UICollectionViewCell
        cell?.backgroundColor = .white
        let title = UILabel(frame: CGRect(x: CGFloat(20), y: CGFloat(20), width: CGFloat((cell?.bounds.size.width)!), height: CGFloat(15)))
        //title.addTextSpacing(1.5)
        title.font = UIFont(name:"GothamRounded-Book", size: 11)

        title.tag = 17
        title.text = instructores[indexPath.row]
        //print("have created an new item \(title.text)")
        cell?.contentView.addSubview(title)

        let txt = UILabel(frame: CGRect(x: CGFloat(15), y: CGFloat(35), width: CGFloat((cell?.bounds.size.width)!), height: CGFloat(10)))
        //title.addTextSpacing(1.5)
        txt.font = UIFont(name:"GothamRounded-Book", size: 11)

        txt.tag = 18
        txt.text = horas[indexPath.row]
        cell?.contentView.addSubview(txt)

    }

    if collectionView == collection2{

        cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell2", for: indexPath) as UICollectionViewCell
        cell?.backgroundColor = .white
        let title = UILabel(frame: CGRect(x: CGFloat(20), y: CGFloat(20), width: CGFloat((cell?.bounds.size.width)!), height: CGFloat(15)))
        //title.addTextSpacing(1.5)
        title.font = UIFont(name:"GothamRounded-Book", size: 11)

        title.tag = 19
        title.text = instructores[indexPath.row]
        cell?.contentView.addSubview(title)

        let txt = UILabel(frame: CGRect(x: CGFloat(15), y: CGFloat(35), width: CGFloat((cell?.bounds.size.width)!), height: CGFloat(10)))
        //title.addTextSpacing(1.5)
        txt.font = UIFont(name:"GothamRounded-Book", size: 11)

        txt.tag = 20
        txt.text = horas[indexPath.row]
        cell?.contentView.addSubview(txt)

    }

    if collectionView == collection3{
        cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell3", for: indexPath) as UICollectionViewCell
        cell?.backgroundColor = .white
        let title = UILabel(frame: CGRect(x: CGFloat(20), y: CGFloat(20), width: CGFloat((cell?.bounds.size.width)!), height: CGFloat(15)))
        //title.addTextSpacing(1.5)
        title.font = UIFont(name:"GothamRounded-Book", size: 11)

        title.tag = 21
        title.text = instructores[indexPath.row]
        cell?.contentView.addSubview(title)

        let txt = UILabel(frame: CGRect(x: CGFloat(15), y: CGFloat(35), width: CGFloat((cell?.bounds.size.width)!), height: CGFloat(10)))
        //title.addTextSpacing(1.5)
        txt.font = UIFont(name:"GothamRounded-Book", size: 11)

        txt.tag = 22
        txt.text = horas[indexPath.row]
        cell?.contentView.addSubview(txt)
    }
    if collectionView == collection4{
        cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell4", for: indexPath) as UICollectionViewCell
        cell?.backgroundColor = .white
        let title = UILabel(frame: CGRect(x: CGFloat(20), y: CGFloat(20), width: CGFloat((cell?.bounds.size.width)!), height: CGFloat(15)))
        //title.addTextSpacing(1.5)
        title.font = UIFont(name:"GothamRounded-Book", size: 11)

        title.tag = 23
        title.text = instructores[indexPath.row]
        cell?.contentView.addSubview(title)

        let txt = UILabel(frame: CGRect(x: CGFloat(15), y: CGFloat(35), width: CGFloat((cell?.bounds.size.width)!), height: CGFloat(10)))
        //title.addTextSpacing(1.5)
        txt.font = UIFont(name:"GothamRounded-Book", size: 11)

        txt.tag = 24
        print("----------------------------------------------------\n\n\(horas)")
        print(horas[indexPath.row])
        txt.text = horas[indexPath.row]
        cell?.contentView.addSubview(txt)
    }

    return cell!
}

// MARK: - COLLECTION VIEW FUNC
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    if collectionView == diasSemana{
        print(dias[indexPath.row])
    }else if collectionView == collection1 {
        print(instructores[indexPath.row])
        self.nombreInstructor = instructores[indexPath.row]
        print(horas[indexPath.row])
        hora = "\(horas[indexPath.row])"
        usrDefaults.set(nombreInstructor, forKey: "INSTRUCTOR")
        performSegue(withIdentifier: "clase_Seleccionada", sender: nil)
        dia = "LUN 30"

    }
    if collectionView == collection2{
        print(instructores2[indexPath.row])
        self.nombreInstructor = instructores2[indexPath.row]
        print(horas2[indexPath.row])
        hora = "\(horas2[indexPath.row])"
        performSegue(withIdentifier: "clase_Seleccionada", sender: nil)
        dia = "MAR 31"


    }
    if collectionView == collection3{
        print(instructores3[indexPath.row])
        self.nombreInstructor = instructores3[indexPath.row]
        print(horas3[indexPath.row])
        hora = "\(horas3[indexPath.row])"
        performSegue(withIdentifier: "clase_Seleccionada", sender: nil)
        dia = "MIE 01"


    }
    if collectionView == collection4{
        print(instructores4[indexPath.row])
        self.nombreInstructor = instructores4[indexPath.row]
        print(horas4[indexPath.row])
        hora = "\(horas4[indexPath.row])"
        performSegue(withIdentifier: "clase_Seleccionada", sender: nil)
        dia = "JUE 02"

    }
}

每次点击上面的按钮(PARK,147,Masaryk)时,它都会刷新。

0 个答案:

没有答案
相关问题