我可能会遗漏一些非常简单的东西,但我无法理解为什么这段代码会给我这么多问题。
有时可行,但大部分时间都会抛出EXC_BAD_ACCESS Code = 1错误。内存地址也始终相同。
为tableview中的每个列调用此方法来创建自定义标头。初始化工作正常,只有在绘图时才会出现问题。 firstLine,secondLine,thirdLine似乎都已被释放。
class MultilineHeaderCell: NSTableHeaderCell {
private let firstLine: String
private let secondLine: String
private let thirdLine: String
private let horizontalPadding: CGFloat = 5.0
private var verticalPadding:CGFloat = 0.0
private var numberOfLines = 0
private var heightOfTextFrame:CGFloat = 0.0
init(HeaderText: String, PrimaryText: String, SecondaryText: String)
{
firstLine = HeaderText
secondLine = PrimaryText
thirdLine = SecondaryText
super.init(textCell: "")
let array = [firstLine, secondLine, thirdLine]
for text in array {
if text.isEmpty == false {
numberOfLines++
heightOfTextFrame = heightOfTextFrame + 21.0
}
}
}
convenience init(HeaderText: String)
{
self.init(HeaderText: HeaderText, PrimaryText: "", SecondaryText: "")
}
convenience init(HeaderText: String, PrimaryText: String)
{
self.init(HeaderText: HeaderText, PrimaryText: PrimaryText, SecondaryText: "")
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func drawWithFrame(cellFrame: NSRect, inView controlView: NSView) {
verticalPadding = (cellFrame.height - heightOfTextFrame) / 2
let textFrame = cellFrame.insetBy(dx: horizontalPadding, dy: verticalPadding)
NSColor(calibratedRed: 217/255, green: 217/255, blue: 217/255, alpha: 1.0).set()
switch firstLine {
case "Olivier" :
NSColor(calibratedRed: 230/255, green: 184/255, blue: 184/255, alpha: 1.0).set()
case "Lyttelton" :
NSColor(calibratedRed: 216/255, green: 228/255, blue: 188/255, alpha: 1.0).set()
case "Dorfman" :
NSColor(calibratedRed: 184/255, green: 204/255, blue: 228/255, alpha: 1.0).set()
case "Temporary Theatre" :
NSColor(calibratedRed: 204/255, green: 198/255, blue: 218/255, alpha: 1.0).set()
default :
NSColor(calibratedRed: 217/255, green: 217/255, blue: 217/255, alpha: 1.0).set()
}
NSBezierPath(rect: cellFrame).fill()
let paraStyle = NSParagraphStyle.defaultParagraphStyle().mutableCopy() as! NSMutableParagraphStyle
paraStyle.alignment = .Center
let font = NSFont.systemFontOfSize(11)
let attributes = [NSForegroundColorAttributeName: NSColor.labelColor(), NSFontAttributeName: font, NSParagraphStyleAttributeName: paraStyle]
let array = [firstLine, secondLine, thirdLine]
switch numberOfLines {
case 1:
for (indexNumber, text) in array.enumerate() {
if text.isEmpty == false {
let yOffset = CGFloat(indexNumber * 21)
text.drawCentredInRectWithYOffset(textFrame, attributes: attributes, yOffset: yOffset)
}
}
case 2:
for (indexNumber, text) in array.enumerate() {
if text.isEmpty == false {
let yOffset = CGFloat(indexNumber * 21)
text.drawCentredInRectWithYOffset(textFrame, attributes: attributes, yOffset: yOffset)
}
}
case 3:
for (indexNumber, text) in array.enumerate() {
if text.isEmpty == false {
let yOffset = CGFloat(indexNumber * 21)
text.drawCentredInRectWithYOffset(textFrame, attributes: attributes, yOffset: yOffset)
}
}
default:
print("NumberOfLines is wrong somehow!")
}
}