我找到了Objective-C这个问题的一些答案,但我对Swift找不到多少。我试图抓住我发现的东西,让它适用于Swift,但我遇到了一个问题:当我添加行self.backgroundColor = UIColor.clearColor()
时,背景应该是它应该是,但是箭头的颜色没有& #39;在突出显示时更改。当该行代码被注释掉时,箭头颜色会改变,但背景是黑色的(我希望它清晰)。以下是我到目前为止班级的内容......
import Foundation
import UIKit
class CustomColoredAccessory: UIControl {
var accessoryColor: UIColor = .lightGrayColor()
var highlightedColor: UIColor = .whiteColor()
init(accessoryColor: UIColor, highlightedColor: UIColor) {
super.init()
self.accessoryColor = accessoryColor
self.highlightedColor = highlightedColor
CGRectMake(0, 0, 11.0, 15.0)
}
override init(frame: CGRect) {
let myFrame = CGRectMake(0, 0, 11.0, 15.0)
super.init(frame: myFrame)
self.backgroundColor = UIColor.clearColor() // This is the line mentioned above
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func drawRect(rect: CGRect) {
// (x,y) is the tip of the arrow
let x = CGRectGetMaxX(self.bounds)-3.0
let y = CGRectGetMidY(self.bounds)
let R: CGFloat = 4.5
var ctxt = UIGraphicsGetCurrentContext()
CGContextMoveToPoint(ctxt, x-R, y-R)
CGContextAddLineToPoint(ctxt, x, y)
CGContextAddLineToPoint(ctxt, x-R, y+R)
CGContextSetLineCap(ctxt, kCGLineCapSquare)
CGContextSetLineJoin(ctxt, kCGLineJoinMiter)
CGContextSetLineWidth(ctxt, 3)
if (self.highlighted) {
self.highlightedColor .setStroke()
} else {
self.accessoryColor .setStroke()
}
CGContextStrokePath(ctxt)
}
}
我正在使用以下行在cellForRowAtIndexPath tableView方法中实现它:
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = "Text"
let accessory: CustomColoredAccessory = CustomColoredAccessory(accessoryColor: UIColor.blackColor(), highlightedColor: UIColor.whiteColor())
cell.accessoryView = accessory
return cell
我也很确定我的初始方法不是犹太人,但我只是把事情扔到了墙上,直到他们陷入困境。任何见解都将不胜感激!