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

时间:2015-11-21 08:40:35

标签: ios swift uitableview

我是Swift的新用户,并使用Table View撰写应用程序。我正在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
}

5 个答案:

答案 0 :(得分:1)

您正在UITableViewCell中添加标签作为子视图但是当重复使用单元格时,这又将标签添加为单元格中的子视图,但之前添加的标签仍然存在,因此您需要在将其添加为子视图之前从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;
}

答案 1 :(得分:1)

dequeueReusableCellWithIdentifier的作用是回收旧单元格,如果它已经创建,则再次使用。所以你的代码所做的就是在单元格中添加子视图,然后滚动它时回收旧单元格并一次又一次地向它添加更多子视图,使其重叠。

您应该做的是设计故事板中的所有子视图,并在cellForRowAtIndexPath中设置数据http://www.raywenderlich.com/50308/storyboards-tutorial-in-ios-7-part-1

答案 2 :(得分:0)

出列的单元格不是SELECT COUNT(*) FROM table_1 FULL OUTER JOIN table_2 ON (table_1.id=table_2.id) 。因此,当重复使用单元格时,您将在已存在的nil之上添加更多UILabel

答案 3 :(得分:0)

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

答案 4 :(得分:0)

这是我在早期iOS Swift开发过程中遇到的最令人沮丧的问题。我尝试了几乎所有东西,但似乎没有什么工作完美经过一个多星期的研究后,我发现这个天堂般的快速开源库在github上名为kingfisher,修复了这个问题。它还使用异步图像缓存,可以使您的应用更快更快地Github link