2016-05-28 7 views
0

CoreDataからデータを保存するためのデータを表示している単一のtableViewがあります。この表は1日分のものであり、その時点で読み取られたセルによってセルがソートされます。私は保存ごとに多くの読み取り値を持っているので、セル内の別のテーブルのように見えるビュー内のサブラベルとしてすべてのデータを表示する単一のセルを持っています(セルアプローチでテーブルを置くために行っていません)。 すべてはうまいですが、場所を保存する場所を表示する痛みの図があります。私はそれが図の正しい場所にポイントを表示するが、これが表示されたら、その日のすべての時間、その図を持つすべてのセルにポイントを追加します。 追加するサブレイヤに特定のセルを指定するにはどうすればよいですか?あなたの答えを得るのでSwift:テーブルビュー内の特定のセルのサブレイヤーで、すべてのセルではありません。

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) 

     } 
    } 
    } 

答えて

0

だけで簡単な概要は、

のUITableViewは、高度に最適化されたため、メモリ内にのみオンスクリーン表示される行を維持しています。現在、すべての行のセルはプールにキャッシュされ、再利用され、再生成されません。ユーザーはUITableViewをスクロールするたびに、Pool内に隠れた行を追加し、次に見える行のためにそれらを再利用します。 サブレイヤを1つのセルに追加すると、もう一度セルが再利用されると、他のセルに同じサブレイヤが表示されます。

だから、今、あなたの方法のcellForRowAtIndexPathであなたの答え

に来て -

は必ず各インデックスの値をリセットします。元のために

条件が真の追加副層である場合、あなたは非常に最高の最適化されているdequeueReusableCellWithIdentifier、しているので、その後サブレイヤ

にSUGGESTION

の言葉を削除しますが、プログラムでサブレイヤを追加しているので、そうでない場合はビューがデキューされた後、サブレイヤが追加されているビューにサブレイヤを追加する可能性が非常に高くなります。

サブレイヤへの参照を取得し、まだ追加されていないかどうかを確認する必要があります。

+0

ありがとう私はまた、おそらく隠されているか可視であるドットイメージを持つことができます。私は今日後であなたの提案を試みます。 – SashaZ

+0

私はcell.painLocationImageView.layer.sublayers?.removeAll()を使用しました。これは不要なものを削除します。しかし、私がしようとしたサブレイヤを参照するにはどうすればよいですか(shapeLayer!= nil){cell.painLocationImageView.layer.sublayers?.removeAll()}それは動作しません – SashaZ

関連する問題