Ios swift:UITableViewCell数据在上下滚动时重叠

Ios swift:UITableViewCell数据在上下滚动时重叠,ios,swift,uitableview,Ios,Swift,Uitableview,我是Swift新手,正在编写一个使用表视图的应用程序。我正在cellForRowAtIndexPath中构建单元格,但当我再次向下和向上滚动时,上一个单元格的数据没有被清除,我在字段上看到重叠的数据。 这是我的密码 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { cellArrayIndex = cellsPointe

我是
Swift
新手,正在编写一个使用
表视图的应用程序。我正在
cellForRowAtIndexPath
中构建单元格,但当我再次向下和向上滚动时,上一个单元格的数据没有被清除,我在字段上看到重叠的数据。 这是我的密码

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    cellArrayIndex = cellsPointerArray[indexPath.row] as! Int

    let cell = self.tableView.dequeueReusableCellWithIdentifier("gameCell",forIndexPath: indexPath) as UITableViewCell

    var indexInGamesArray = 0
    let btnWidth = CGFloat((cell.frame.width / 100) * 20)
    let teamSignWidth = CGFloat((cell.frame.width / 100) * 4)
    let gapWidth = CGFloat((cell.frame.width / 100) * 3)

    //***** fixet fields iv every cell
    let lblTop = UILabel(frame: CGRectMake(0 ,0,self.frame.size.width, 25))
    lblTop.backgroundColor = UIColor(red: 88/255, green: 88/255, blue: 88/255, alpha: 1)
    cell.addSubview(lblTop)

    let lblDate = UILabel(frame: CGRectMake(lblTop.frame.origin.x + 30.0,lblTop.frame.origin.y,100,lblTop.frame.size.height))
    lblDate.backgroundColor = UIColor.clearColor()

    let lblTime = UILabel(frame: CGRectMake(lblDate.frame.origin.x + lblDate.frame.size.width + 30.0,lblTop.frame.origin.y,300,lblTop.frame.size.height))
    lblTime.backgroundColor = UIColor.clearColor()

    let str : String = (gamesArray[cellArrayIndex]["GameDate"] as? String)!
    let characterToFind: Character = "T"
    let indexForCharacterInString = str.characters.indexOf(characterToFind)
    let dateStr:String = str.substringToIndex(indexForCharacterInString!)
    lblDate.text = dateStr
    lblDate.textColor = UIColor.yellowColor()
    cell.addSubview(lblDate)

    var timeStr:String = str.substringFromIndex(indexForCharacterInString!)
    timeStr.removeAtIndex(str.characters.indices.first!)
    lblTime.text = timeStr
    lblTime.textColor = UIColor.yellowColor()
    cell.addSubview(lblTime)

    let lblHomName = UILabel(frame: CGRectMake(20,lblDate.frame.origin.y + lblDate.frame.size.height,self.frame.size.width/2 - 30,25))
    lblHomName.font = const.smallFont
    lblHomName.text = gamesArray[cellArrayIndex]["HomeName"] as? String


    let HcapLine = gamesArray[cellArrayIndex]["HcapLine"] as? Int
    if(HcapLine < 0){
        lblHomName.textColor = UIColor.redColor()
    }
    else{
        lblHomName.textColor = const.lblColorSTD
    }
    cell.addSubview(lblHomName)

    let lblAwayName = UILabel(frame: CGRectMake(self.frame.width/2 + 20,lblDate.frame.origin.y + lblDate.frame.size.height,self.frame.size.width/2 - 30,25))
    lblAwayName.font = const.smallFont
    lblAwayName.text = gamesArray[cellArrayIndex]["AwayName"] as? String

    if(HcapLine < 0){
        lblAwayName.textColor = UIColor.redColor()
    }
    else{
        lblAwayName.textColor = const.lblColorSTD
    }
    cell.addSubview(lblAwayName)

    let lblHC = UILabel(frame: CGRectMake(cell.frame.width/2 - 80,lblAwayName.frame.origin.y + lblAwayName.frame.size.height,self.frame.size.width/2 - 30,25))
    lblHC.text = "Handicap"
    lblHC.textColor = UIColor.redColor()
    lblHC.font = const.smallBoldFont
    cell.addSubview(lblHC)

    let lblTL = UILabel(frame: CGRectMake(cell.frame.width/2 + 40,lblAwayName.frame.origin.y + lblAwayName.frame.size.height,self.frame.size.width/2 - 30,25))
    lblTL.text = "Total Line"
    lblTL.textColor = UIColor.redColor()
    lblTL.font = const.smallBoldFont
    cell.addSubview(lblTL)

    //***** end if fixed fields block

    //***************************************
    //***** Build the lines of the cell *****
    //***************************************
print("gamesArray.count: ",gamesArray.count)
    let NumberOfLines = numOfGamesArray[indexPath.row] as! Int

    indexInGamesArray = (cellsPointerArray[indexPath.row] as! Int) //- 1
    print("indexPath.row:",indexPath.row)
    print("NumberOfLines:" ,NumberOfLines)


    for(var i = 0;i <= NumberOfLines; i++){

        let lineMultiplayer = CGFloat(i)

        let gameView = UIView(frame: CGRectMake(0,CGFloat(i * 60) + lblTL.frame.origin.y + lblTL.frame.height ,cell.frame.width,60))

        let lblH = UILabel(frame: CGRectMake(0,lineMultiplayer * 25,teamSignWidth,25))
        print ("blH.frame.origin.y: ",lblH.frame.origin.y)
        lblH.text = " H"
        lblH.textColor = const.lblColorSTD
        lblH.font = const.smallFont
        gameView.addSubview(lblH)

        let lblHcapLine = UILabel(frame: CGRectMake(lblH.frame.origin.x + lblH.frame.width, lblH.frame.origin.y,btnWidth,25))
        lblHcapLine.layer.borderColor = const.lblColorSTD.CGColor
        lblHcapLine.layer.borderWidth = 1
        lblHcapLine.textColor = const.lblColorSTD
        lblHcapLine.font = const.stdLblFont
        print("gamesArray[indexInGamesArray][\"HcapLine\"]:" ,gamesArray[indexInGamesArray]["HcapLine"])
        print("indexInGamesArray: ",indexInGamesArray)
        lblHcapLine.text = (gamesArray[indexInGamesArray]["HcapLine"])!!.description
        lblHcapLine.textAlignment = NSTextAlignment.Center
        gameView.addSubview(lblHcapLine)

        cell.addSubview(gameView)
         indexInGamesArray++

    }
    cellArrayIndex = 0

    //***** Build the lines of the cell block end

    return cell
}
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
CellrayIndex=CellsInterArray[indexath.row]as!Int
让cell=self.tableView.dequeueReusableCellWithIdentifier(“gameCell”,forIndexPath:indexPath)作为UITableViewCell
var indexInGamesArray=0
设btnWidth=CGFloat((cell.frame.width/100)*20)
让teamSignWidth=CGFloat((cell.frame.width/100)*4)
设间隙宽度=CGFloat((cell.frame.width/100)*3)
//*****fixet字段iv每个单元格
设lblTop=UILabel(帧:CGRectMake(0,0,self.frame.size.width,25))
lblTop.backgroundColor=UIColor(红色:88/255,绿色:88/255,蓝色:88/255,alpha:1)
cell.addSubview(lblTop)
设lblDate=UILabel(frame:CGRectMake(lblTop.frame.origin.x+30.0,lblTop.frame.origin.y,100,lblTop.frame.size.height))
lblDate.backgroundColor=UIColor.clearColor()
设lblTime=UILabel(frame:CGRectMake(lblDate.frame.origin.x+lblDate.frame.size.width+30.0,lblTop.frame.origin.y,300,lblTop.frame.size.height))
lblTime.backgroundColor=UIColor.clearColor()
让str:String=(gamesArray[CellaryIndex][“GameDate”]作为?字符串)!
让characterToFind:Character=“T”
让indexforcharacteristing=str.characters.indexOf(characterToFind)
让dateStr:String=str.substringToIndex(indexforcharacteristing!)
lblDate.text=dateStr
lblDate.textColor=UIColor.yellowColor()
cell.addSubview(lblDate)
var timeStr:String=str.substringFromIndex(indexForCharacteristing!)
timeStr.removeAtIndex(str.characters.index.first!)
lblTime.text=timeStr
lblTime.textColor=UIColor.yellowColor()
cell.addSubview(lblTime)
让lblHomName=UILabel(frame:CGRectMake(20,lblDate.frame.origin.y+lblDate.frame.size.height,self.frame.size.width/2-30,25))
lblHomName.font=const.smallFont
lblHomName.text=gamesArray[cillarrayindex][“HomeName”]as?字符串
设HcapLine=gamesArray[cillarrayindex][“HcapLine”]as?Int
如果(HcapLine<0){
lblHomName.textColor=UIColor.redColor()
}
否则{
lblHomName.textColor=const.lblColorSTD
}
cell.addSubview(lblHomName)
设lblAwayName=UILabel(frame:CGRectMake(self.frame.width/2+20,lblDate.frame.origin.y+lblDate.frame.size.height,self.frame.size.width/2-30,25))
lblAwayName.font=const.smallFont
lblAwayName.text=gamesArray[CellaryIndex][“AwayName”]as?字符串
如果(HcapLine<0){
lblAwayName.textColor=UIColor.redColor()
}
否则{
lblAwayName.textColor=const.lblColorSTD
}
cell.addSubview(lblAwayName)
设lblHC=UILabel(frame:CGRectMake(cell.frame.width/2-80,lblAwayName.frame.origin.y+lblAwayName.frame.size.height,self.frame.size.width/2-30,25))
lblHC.text=“障碍”
lblHC.textColor=UIColor.redColor()
lblHC.font=const.smallBoldFont
cell.addSubview(lblHC)
设lblTL=UILabel(frame:CGRectMake(cell.frame.width/2+40,lblAwayName.frame.origin.y+lblAwayName.frame.size.height,self.frame.size.width/2-30,25))
lblTL.text=“总行”
lblTL.textColor=UIColor.redColor()
lblTL.font=const.smallBoldFont
cell.addSubview(lblTL)
//*****如果固定字段阻塞,则结束
//***************************************
//*****建立细胞系*****
//***************************************
打印(“gamesArray.count:,gamesArray.count”)
让NumberOfLines=nummofgamesarray[indexPath.row]作为!Int
indexInGamesArray=(cellsPointerArray[indexPath.row]as!Int)/-1
打印(“indexPath.row:,indexPath.row”)
打印(“NumberOfLines:”,NumberOfLines)

对于(var i=0;i您正在将标签作为子视图添加到uitableview单元格中,但当单元格被重用时,这会再次将标签作为子视图添加到单元格中,但您之前添加的标签仍然存在,因此您需要在将其作为子视图添加之前从uitableview单元格中删除标签

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    cellArrayIndex = cellsPointerArray[indexPath.row] as! Int

    let cell = self.tableView.dequeueReusableCellWithIdentifier("gameCell",forIndexPath: indexPath) as UITableViewCell

    var indexInGamesArray = 0
    let btnWidth = CGFloat((cell.frame.width / 100) * 20)
    let teamSignWidth = CGFloat((cell.frame.width / 100) * 4)
    let gapWidth = CGFloat((cell.frame.width / 100) * 3)

    //***** fixet fields iv every cell
    let lblTop = UILabel(frame: CGRectMake(0 ,0,self.frame.size.width, 25))
    lblTop.backgroundColor = UIColor(red: 88/255, green: 88/255, blue: 88/255, alpha: 1)

   for let view in cell.subviews
   {
      if view isKindOfClass: [UILabel Class]
      {
          [view removeFromSuperview];
        }

    }

    cell.addSubview(lblTop)


   return cell;
}

dequeueReusableCellWithIdentifier
所做的是回收旧单元格,以便在已创建的情况下再次使用。因此,代码所做的是向单元格中添加子视图,然后在滚动时回收旧单元格,并一次又一次地向其添加更多子视图,使其重叠


您应该做的是设计故事板中的所有子视图,并在
cellForRowAtIndexPath
中设置数据出列单元格不是
nil
。因此,当单元格被重用时,您将在已经存在的
UILabel
之上添加更多的
UILabel

首先,谢谢大家,all答案中包含了解决方案,我结束了从单元格中删除所有子视图的过程,因为它是一个动态单元格,可以容纳不同数量的字段,我不知道有多少字段
再次感谢大家!

这是我在早期iOS Swift开发过程中遇到的最令人沮丧的问题。我尝试了几乎所有的方法,但没有一种方法能够完美地工作。经过一周多的研究,我在github上找到了一个名为kingfisher的天堂般的Swift开源库,它解决了这个问题。它还使用了异步映像cache这将使你的应用程序更快

我正在进行清理,因为每个单元格都有不同的高度和不同的字段数,正如你在代码中看到的那样