2016-08-22 45 views
0

我一直有这个问题数周,我想我无法解决它。UITableViewCell得到重用,并显示UIView中的错误绘图

我有一个tableView它有自定义tableViewCell s和他们充满了数据。 tableViewCell有两个UILabel和一个UIView

问题出现在我滚动几次UIView上的图纸重叠时,我认为它们被重新绘制。我知道这种行为是因为细胞的重用,但我甚至找不到问题的根源。

Before scrolling

UIViews显示完美开启应用程式

After scrolling

UIViews获得滚动后重叠

UITableViewcellForRowAtIndexPath是:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let CellIdentifier = "Cell" 

    let cell: CustomTableViewcell = self.tableView.dequeueReusableCellWithIdentifier(CellIdentifier, forIndexPath: indexPath) as! CustomTableViewcell 
            //self.tableView.dequeueReusableCellWithIdentifier(CellIdentifier) as! CustomTableViewcell 

    cell.prepareForReuse() 

    self.createUIForCell(cell) 
    self.configureCell(cell, indexPath: indexPath) 

    print("\t\(parsedData[indexPath.row].stationName): \(parsedData[indexPath.row].freeBikes)") 

    return cell 
} 

func createUIForCell(cell: CustomTableViewcell) { 

    cell.distanceLabel.textColor  = UIColor.whiteColor() 
    cell.distanceLabel.backgroundColor = UIColor.clearColor() 
    cell.bikeStationLabel.textColor = UIColor.whiteColor() 
    cell.bikeStationLabel.backgroundColor = UIColor.clearColor() 
} 

func configureCell(cell: CustomTableViewcell, indexPath: NSIndexPath) { 

    let stations = parsedData[indexPath.row] 

    if indexPath.row % 2 == 0 { 
     cell.stackView.arrangedSubviews.first!.backgroundColor = cellBackgroundColor1 
     cell.progressView.backgroundColor = cellBackgroundColor2 
    } else { 
     cell.stackView.arrangedSubviews.first!.backgroundColor = cellBackgroundColor2 
     cell.progressView.backgroundColor = cellBackgroundColor1 
    } 

    cell.progressView.getWidth(stations.freeBikes, freeDocks: stations.freeDocks) 

    cell.getLocation(stations.latitude, longitude: stations.longitude) 
    cell.getParameters(stations.freeBikes, freeDocks: stations.freeDocks) 
    cell.bikeStationLabel.text = stations.stationName 

    if stations.distanceToLocation != nil { 
     if stations.distanceToLocation! < 1000 { 
      cell.distanceLabel.text = String(format: "%.f m", stations.distanceToLocation!) 
     } else { 
      cell.distanceLabel.text = String(format: "%.1f km", stations.distanceToLocation!/1000) 
     } 
    } else { 
     cell.distanceLabel.text = "" 
    } 
} 

对于提到UIVIew我的自定义单元格内之前,我已经创建了一个分隔的类为它处理的绘图和它看起来像这样:

import UIKit 

class ProgressViewBar: UIView { 

var freeBikes = 0 
var freeDocks = 0 
var text: UILabel? = nil 
var text2: UILabel? = nil 

func getWidth(freeBikes: Int, freeDocks: Int) { 

    self.freeBikes = freeBikes 
    self.freeDocks = freeDocks 
} 

override func drawRect(rect: CGRect) { 

    let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: rect.width * (CGFloat(freeBikes)/(CGFloat(freeBikes) + CGFloat(freeDocks))), height: frame.height), cornerRadius: 0) 
    let shapeLayer = CAShapeLayer() 
    shapeLayer.path = path.CGPath 
    shapeLayer.strokeColor = UIColor.whiteColor().CGColor 
    shapeLayer.fillColor = UIColor.whiteColor().CGColor 

    self.layer.addSublayer(shapeLayer) 

    drawText(rect) 
} 

func drawText(rect: CGRect) { 


    if freeBikes != 0 { 
     text?.removeFromSuperview() 
     text = UILabel(frame: CGRect(x: 2, y: 0, width: 20, height: 20)) 
     text!.text = String("\(freeBikes)") 
     text!.textAlignment = NSTextAlignment.Left 
     text!.font = UIFont(name: "HelveticaNeue-Thin", size: 17) 
     text!.backgroundColor = UIColor.clearColor() 
     text!.textColor = UIColor(red: 1/255, green: 87/255, blue: 155/255, alpha: 1) 

     self.addSubview(text!) 
    } 

    text2?.removeFromSuperview() 
    text2 = UILabel(frame: CGRect(x: rect.width * (CGFloat(freeBikes)/(CGFloat(freeBikes) + CGFloat(freeDocks))) + 2, y: 0, width: 20, height: 20)) 
    text2!.text = String("\(freeDocks)") 
    text2!.textAlignment = NSTextAlignment.Left 
    text2!.font = UIFont(name: "HelveticaNeue-Thin", size: 17) 
    text2!.backgroundColor = UIColor.clearColor() 
    text2!.textColor = UIColor.whiteColor() 
    self.addSubview(text2!) 
} 
} 

视图需求两个要绘制的参数,我使用func getWidth(freeBikes: Int, freeDocks: Int)方法从`ViewController调用它来传递它们。如果需要其他任何代码,您可以查看repo

回答

1

问题是,当重复使用单元格时,再次调用drawRect并且不清除已经绘制的矩形。在绘制另一个之前清除它。

+0

使shapeLayer成为一个类的属性,然后在创建'shapeLayer.removeFromSuperlayer()'和'shapeLayer = nil'之前。在做之前检查它是否不是零。 –