【问题标题】:Swift: addSublayer in specific cell in Tableview not every cellSwift:Tableview 中特定单元格中的 addSublayer 不是每个单元格
【发布时间】:2016-05-28 01:26:00
【问题描述】:

我有一个 tableView 显示用于保存来自 CoreData 的数据的数据。该表是一天的,单元格按当时的读数排序。每次保存我有很多读数,所以我有一个单元格,它在视图中将所有数据显示为子标签,看起来像单元格中的另一个表格(不适合将表格放在单元格方法中)。 一切都很好,但我有一个疼痛图,显示您保存位置的位置。我让它在图表上的正确位置显示点,但是当它出现时,它会在当天的所有时间将点添加到具有该图表的每个单元格。 如何为要添加的子图层指定特定单元格?

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  let OurData = people[indexPath.row] as! UserData

    let cell: CalenderDataTableViewCell = tableView.dequeueReusableCellWithIdentifier("dataCalenderCell") as! CalenderDataTableViewCell

    cell.backgroundColor = GlobalColourConstants.tableViewBackground


// MARK: diagram cell

// LocationX and LocationY represent the decimal ration of how far from the origin the point is e.g. (locationX: 0.5, locationY: 0.5) = centrePoint 

  if let locationX: Float = OurData.painDiagramLocationX {
    print("locationx \(locationX)")

    if locationX == 0 {
      cell.painLocationBackgroundView.layer.borderColor = GlobalColourConstants.noteColourArray[0].CGColor
      cell.painLocationBackgroundView.layer.backgroundColor = UIColor.clearColor().CGColor

// This cell.painLocationImageView = nil is suppose to remove image from cells that don't have a pain diagram. However it crashes the table.

//          cell.painLocationImageView = nil 
    } else {

      let imageName = "Torso Female"
      let image = UIImage(named: imageName)
      cell.painLocationImageView.image = image

      if let locationY: Float = OurData.painDiagramLocationY {
        cell.painLocationBackgroundView.layer.borderColor = GlobalColourConstants.painColourArray[0].CGColor
        cell.painLocationBackgroundView.layer.backgroundColor = GlobalColourConstants.painColourArray[0].CGColor

        print("locationy \(locationY)")

        let pictureOriginX = (cell.painLocationImageView.bounds.origin.x)
        let pictureOriginY = (cell.painLocationImageView.bounds.origin.y)

        let pointOriginX = cell.painLocationImageView.bounds.width * CGFloat(locationX)
        let pointOriginY = cell.painLocationImageView.bounds.height * CGFloat(locationY)

        let circleRadius = CGFloat(8)

        let circlePath = UIBezierPath(arcCenter: CGPoint(x: pointOriginX,y: pointOriginY), radius: circleRadius, startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)

        let shapeLayer = CAShapeLayer()
        shapeLayer.path = circlePath.CGPath

        //change the fill color
        shapeLayer.fillColor = UIColor.redColor().CGColor
        //you can change the stroke color
        shapeLayer.strokeColor = UIColor.whiteColor().CGColor
        //you can change the line width
        shapeLayer.lineWidth = 1.0

        cell.painLocationImageView.layer.addSublayer(shapeLayer)

      }
    }
  }

【问题讨论】:

    标签: ios swift uitableview addsubview


    【解决方案1】:

    只是一个简短的概述,所以你得到你的答案

    UITableView 高度优化,因此只在内存中保留屏幕上可见的行。现在,所有行 Cell 都缓存在 Pool 中,并且可以重复使用而不是重新生成。每当用户滚动 UITableView 时,它会将刚刚隐藏的行添加到 Pool 中,并将它们重用于下一个可见行。 所以你在一个单元格中添加子层,一旦单元格被重用,你会在另一个单元格中看到相同的子层。

    那么,现在来回答你的问题

    在你的方法 cellForRowAtIndexPath --

    始终重置每个索引的值:

    例如。 如果条件为真则添加子层,如果不是则删除子层

    一个建议

    由于您是 dequeueReusableCellWithIdentifier,这是最优化的,但是由于您以编程方式添加子层,因此在视图被出列后,您很有可能在视图上添加子层,这些子层已经有子层已添加。

    您应该获取对子图层的引用,并检查它是否尚未添加。

    【讨论】:

    • 谢谢我也可以有一个隐藏或可见的点图像。今天晚些时候我会试试你的建议。
    • 我使用了 cell.painLocationImageView.layer.sublayers?.removeAll() 并删除了不需要的。但是,如果 (shapeLayer != nil) { cell.painLocationImageView.layer.sublayers?.removeAll() } 并且不起作用,我如何引用我尝试过的子层
    猜你喜欢
    • 2019-04-21
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 2020-08-23
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多