重用单元格时,UICollectionViewCell 的文本出现在其他地方

UICollectionViewCell's text is appearing somewhere else when the cell is reused

我正在使用 swift 制作一个时间表应用程序。 我从网站加载我的数据并将其放在 collectionView 上。 在第一部分中,我输入了星期几,在第一行中,我输入了 classes,在其他单元格中,我输入了来自网络的数据(主题名称,class 房间。 ..) 如果我进入时间表视图,一开始一切都很好。 但是,如果我向下滚动和向上滚动,一些文本会打印在它们不应该出现的地方。

这是我的collectionView(collectionView: , cellForItemAtIndexPath: )

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    print("\(indexPath.section), \(indexPath.row)")
    let dayArray = ["", "월", "화", "수", "목", "금"]
    if indexPath.section == 0 {
        // Day
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell
        var titleLabel = UILabel(frame: CGRectMake(0, 0, cell.bounds.width, cell.bounds.height))
        cell.contentView.addSubview(titleLabel)
        titleLabel.text = dayArray[indexPath.row]
        titleLabel.textAlignment = NSTextAlignment.Center
        cell.backgroundColor = mainColor
        print("day")
        return cell
    }
    if indexPath.row == 0 {
        // index
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell
        var titleLabel = UILabel(frame: CGRectMake(0, 0, cell.bounds.width, cell.bounds.height))
        cell.contentView.addSubview(titleLabel)
        titleLabel.text = String(indexPath.section)
        titleLabel.textAlignment = NSTextAlignment.Center
        cell.backgroundColor = mainColor
        print("time")
        return cell
    }
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! TimeTableCell
    if let timeTableElement = self.timeTable[indexPath.section-1][indexPath.row-1] {
        cell.subjectNameLabel.text = timeTableElement.subjectName
        cell.subjectNameLabel.adjustsFontSizeToFitWidth = true
        cell.classRoomLabel.text = timeTableElement.classRoom
        cell.classRoomLabel.adjustsFontSizeToFitWidth = true
    } else {
        cell.subjectNameLabel.text = ""
        cell.classRoomLabel.text = ""
    }
    cell.backgroundColor = mainColor
    print("class")
    return cell
}

我使用默认 UICollectionViewCell 添加标签子视图来显示日期和 class 时间,并使用自定义 TimeTableCell 来显示 class 数据.

我该如何解决这个问题?

尝试为 TimeTableCell 使用不同的 'reuseIdentifier':

  1. 更改属性检查器中的重用标识符:
  2. 更改您的代码:

     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(yourReuseIdentifier, forIndexPath: indexPath) as! TimeTableCell
    

ps。我建议先去掉已有的titleLabel,再添加新的titleLabel,否则上下滚动时会重新添加。

编辑

您可以通过两种方式'delete' titleLabel:

  1. 添加自定义单元格以及 TimeTableCell(不同的名称和不同的 ReuseIdentifier)。如您所见,TimeTableCell中的subjectNameLabel不会被重复添加。

  2. 初始化后给titleLabel设置一个tag,根据tag去掉。

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell
    cell.contentView.viewWithTag(999)!.removeFromSuperview()
    var titleLabel = UILabel(frame: CGRectMake(0, 0, cell.bounds.width, cell.bounds.height))
    titleLabel.tag = 999
    cell.contentView.addSubview(titleLabel)
    titleLabel.text = dayArray[indexPath.row]
    titleLabel.textAlignment = NSTextAlignment.Center
    cell.backgroundColor = mainColor
    print("day")
    return cell
    

您需要在添加之前删除标题标签

 let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell
     // Here find view with tag 999 and remove it from superview 
    var titleLabel = UILabel(frame: CGRectMake(0, 0, cell.bounds.width, cell.bounds.height))
    cell.contentView.addSubview(titleLabel)
    titleLabel.tag = 999;
    titleLabel.text = String(indexPath.section)
    titleLabel.textAlignment = NSTextAlignment.Center
    cell.backgroundColor = mainColor
    print("time")
    return cell