【发布时间】:2018-01-14 22:17:15
【问题描述】:
我正在尝试在我的表格视图的每一行中设置此自定义视图。
在我的界面构建器中,我在 UITableViewCell 中添加了一个 UIView,并链接了 IBOutlet。所以我将其子类化为CircleView
这是它的类
class CircleView: UIView {
var shapeLayer = CAShapeLayer()
override func draw(_ rect: CGRect) {
super.draw(rect)
let circlePath = UIBezierPath(arcCenter: CGPoint(x: rect.width/2,y: rect.height/2), radius: CGFloat(20), startAngle: CGFloat(0), endAngle:CGFloat(Double.pi*2), clockwise: true)
shapeLayer.path = circlePath.cgPath
shapeLayer.fillColor = Constants.defaultCircleFillColor
shapeLayer.strokeColor = UIColor.clear.cgColor
shapeLayer.lineWidth = 1.5
self.layer.addSublayer(shapeLayer)
}
func setColor(_ color: UIColor) {
shapeLayer.strokeColor = color.cgColor
}
}
但是当我尝试在cellForRowAt 委托中调用setColor 函数时,什么也没有发生。
有没有办法管理它的笔触颜色?
这是cellForRowAt:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let remote_row: RemoteType = sections[indexPath.section].cellData[indexPath.row]
if let cell = tableView.dequeueReusableCell(withIdentifier: remote_row.identifier) as? RemoteCell {
switch remote_row {
case .Item(circleColor: let color, text: let title):
cell.configCell(circleColor: color, description: title)
}
return cell
} else {
fatalError("Unknown identifier")
}
}
和 RemoteCell:
class RemoteCell: UITableViewCell {
@IBOutlet weak var title: UILabel!
@IBOutlet weak var circleView: CircleView!
@IBOutlet weak var settingButton: UIButton!
func configCell(circleColor: UIColor, description: String) {
title.text = description
circleView.setColor(UIColor.blue)
}
}
【问题讨论】:
-
您应该包含
cellForRowAt的代码,因为错误很可能存在于那里,而不是自定义CircleView类本身。 -
也许
setColor在draw之前被调用。请尝试使用日志或断点进行检查。 -
您确定没有调用该方法吗?你有没有在方法中设置断点来查看它是否被调用?
-
我添加了断点...问题是
setColor在draw之前被调用 -
@CrazyDev 请包含
cellForRowAt的代码。我很确定问题在于没有正确初始化视图,而只是尝试将UIView转换为CircleView,但没有相关代码,我无法确定。
标签: ios swift3 uiview tableview subclass