【发布时间】: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