Swift 具有自定义视图的UITableViewCell
我正在拼命尝试向UITableViewCell添加自定义视图。我在故事板中有一个UITableViewController,它链接到一个TestTableViewController类,原型单元的标识符为“Cell2” 这是UITableViewController中的代码:Swift 具有自定义视图的UITableViewCell,swift,uitableview,user-interface,Swift,Uitableview,User Interface,我正在拼命尝试向UITableViewCell添加自定义视图。我在故事板中有一个UITableViewController,它链接到一个TestTableViewController类,原型单元的标识符为“Cell2” 这是UITableViewController中的代码: override func numberOfSectionsInTableView(tableView: UITableView) -> Int { // #warning Incomplete im
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return dataArray.count
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 190
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath)
let data = dataArray[indexPath.row]
let ticket = YellowTackleTicketComplete()
ticket.frame = CGRect(x: 20, y: 20, width: 335, height: 150)
cell.addSubview(ticket)
// cell.textLabel!.text = tackl!.valueForKey("title") as? String
cell.backgroundColor = UIColor.clearColor()
print(cell.subviews)
return cell
}
dataArray仅用于测试目的,其中有一项。所以我得到的是一个有一个空单元格的表视图。在Xcode UI调试器中,我可以看到自定义视图。但它并没有显示在模拟器或设备上
如果有人能帮忙,非常感谢
这是视图的代码:
class YellowTackleTicketComplete: UIView {
var containerView: UIView!
var ticket: TacklTicket!
var dropDownMenu: YellowDrawer!
var dropDownBackground: YellowDrawerBackground!
var ticketShadowLine: UIView!
var lowAlphaView: UIView!
var outbackButton: UIButton!
var arrowView: UIView!
var bounceHeight: CGFloat?
var dropdownHeight: CGFloat?
var topBorder: CGFloat?
var bottomBorder: CGFloat?
//let arrowLayer = CALayer()
//let layerDelegate = LayerDelegate()
var dropDownElements = [String]()
var dropped = false
var animating = false
var delegate: YellowTackleTicketCompleteDelegate?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.addObserver(self, forKeyPath: "highlighted", options: NSKeyValueObservingOptions.New, context: nil)
}
override init(frame: CGRect) {
super.init(frame: frame)
self.addObserver(self, forKeyPath: "highlighted", options: NSKeyValueObservingOptions.New, context: nil)
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
self.setNeedsDisplay()
if keyPath == "frame" {
// Set up DropdownMenu
self.dropDownBackground.frame.size.height = self.dropDownMenu.frame.maxY
}
}
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
if(!self.clipsToBounds && !self.hidden && self.alpha > 0.0){
let subviews = self.subviews.reverse()
for member in subviews {
let subPoint = member.convertPoint(point, fromView: self)
if let result:UIView = member.hitTest(subPoint, withEvent:event) {
return result;
}
}
}
return nil
}
func ticketTapped() {
//if !animating {
if !dropped {
showDropdown()
NSLog("#")
// NSLog("Scrolling to \((dropDownMenu.indexPathForSelectedRow?.row)!)(the index of the tableview selection)")
// dropDownMenu.scrollToRowAtIndexPath((dropDownMenu.indexPathForSelectedRow)!, atScrollPosition: .Middle, animated: false)
} else {
hideDropdown()
}
// }
}
func showDropdown() {
dropDownMenu.hidden = false
self.ticket.drawShadowBeneathTicket()
print("showing")
if !animating {
animating = true
UIView.animateWithDuration(
1.0,
delay: 0,
usingSpringWithDamping: 0.7,
initialSpringVelocity: 0.5,
options: [],
animations: {
self.dropDownMenu.frame.origin.y = self.bottomBorder!
}, completion: { _ in
//self.delegate?.menuOpen?()
self.animating = false
print(self.dropDownBackground.frame.size.height)
print(self.dropDownMenu.frame.maxY)
}
)
}
self.setNeedsDisplay()
self.dropDownMenu!.setNeedsDisplay()
dropped = true
}
func hideDropdown() {
if !animating {
animating = true
UIView.animateWithDuration(
0.7,
delay: 0,
usingSpringWithDamping: 0.7,
initialSpringVelocity: 0.5,
options: [],
animations: {
self.dropDownMenu.frame.origin.y = self.topBorder! + self.bounceHeight!
}, completion: nil
)
}
UIView.animateWithDuration(
0.5,
delay: 0,
options: UIViewAnimationOptions.TransitionNone,
animations: {
self.dropDownMenu.frame.origin.y = self.topBorder! - self.dropdownHeight!
}, completion: { _ in
//self.delegate?.menuClosed?()
self.animating = false
self.dropDownMenu.hidden = true
self.ticket.setNeedsDisplay()
self.setNeedsDisplay()
}
)
dropped = false
}
func initSubViews() {
self.topBorder = self.frame.height*6/150
self.bottomBorder = self.frame.height - self.topBorder!
self.bounceHeight = 250
self.dropdownHeight = 350
containerView = UIView()
containerView.frame = CGRect(x: 0, y: topBorder!, width: self.bounds.width, height: dropdownHeight!+bounceHeight!)
containerView.clipsToBounds = true
ticket = TacklTicket()
ticket.frame = self.bounds
ticket.addTarget(self, action: #selector(YellowTackleTicketComplete.ticketTapped), forControlEvents: .TouchDown)
dropDownMenu = YellowDrawer()
dropDownMenu.frame = CGRect(x: 0, y: topBorder! - dropdownHeight!, width: containerView.bounds.width, height: dropdownHeight!)
dropDownMenu.hidden = true
dropDownMenu.backgroundColor = UIColor.clearColor()
dropDownMenu.addObserver(self, forKeyPath: "frame", options: .New, context: nil)
dropDownBackground = YellowDrawerBackground()
dropDownBackground.frame = CGRectMake(0,0,self.bounds.width,self.dropDownMenu.frame.maxY)
self.addSubview(containerView)
containerView.addSubview(dropDownBackground)
containerView.addSubview(dropDownMenu)
self.addSubview(ticket)
}
}
类YellowTackleTicketComplete:UIView{
var-containerView:UIView!
var票:塔克提克!
var下拉菜单:黄色抽屉!
var下拉背景:黄色抽屉背景!
var ticketShadowLine:UIView!
var lowAlphaView:UIView!
var outbackButton:UIButton!
var arrowView:UIView!
var弹跳高度:CGFloat?
var下拉高度:CGFloat?
var topBorder:CGFloat?
var bottomBorder:CGFloat?
//设arrowLayer=CALayer()
//设layerDelegate=layerDelegate()
var dropDownElements=[String]()
var=false
var动画设置=false
var代表:YellowTackleTicketCompleteDelegate?
必需的初始化?(编码器aDecoder:NSCoder){
super.init(编码者:aDecoder)
addObserver(self,forKeyPath:“突出显示”,选项:NSKeyValueObservingOptions.New,上下文:nil)
}
重写初始化(帧:CGRect){
super.init(frame:frame)
addObserver(self,forKeyPath:“突出显示”,选项:NSKeyValueObservingOptions.New,上下文:nil)
}
重写func observeValueForKeyPath(键路径:String?,对象对象的类型:AnyObject?,更改:[String:AnyObject]?,上下文:UnsafeMutablePointer){
self.setNeedsDisplay()
如果keyPath==“帧”{
//设置下拉菜单
self.dropDownBackground.frame.size.height=self.dropDownMenu.frame.maxY
}
}
覆盖func hitTest(点:CGPoint,withEvent事件:UIEvent?->UIView{
如果(!self.clipsToBounds&&!self.hidden&&self.alpha>0.0){
让子视图=self.subviews.reverse()
对于子视图中的成员{
让subPoint=member.convertPoint(point,fromView:self)
如果let结果:UIView=member.hitTest(子点,withEvent:event){
返回结果;
}
}
}
归零
}
func ticketTapped(){
//如果!动画{
如果!掉了{
摊牌
NSLog(“#”)
//NSLog(“滚动到\((dropDownMenu.indexPathForSelectedRow?.row)!)(tableview选择的索引)”)
//dropDownMenu.ScrollToRowatinedExath((dropDownMenu.indexPathForSelectedRow)!,在ScrollPosition:中间,动画:false)
}否则{
hideDropdown()
}
// }
}
func showDropdown(){
dropDownMenu.hidden=false
self.ticket.drawShadowBeneathTicket()
打印(“显示”)
如果!动画{
设置动画=真
UIView.animateWithDuration(
1.0,
延迟:0,
使用带阻尼的弹簧:0.7,
初始速度:0.5,
选项:[],
动画:{
self.dropDownMenu.frame.origin.y=self.bottomBorder!
},完成:{uuuin
//self.delegate?.menuOpen?()
self.animating=false
打印(self.dropDownBackground.frame.size.height)
打印(self.dropDownMenu.frame.maxY)
}
)
}
self.setNeedsDisplay()
self.dropdown菜单!.setNeedsDisplay()
删除=真
}
func hideDropdown(){
如果!动画{
设置动画=真
UIView.animateWithDuration(
0.7,
延迟:0,
使用带阻尼的弹簧:0.7,
初始速度:0.5,
选项:[],
动画:{
self.dropDownMenu.frame.origin.y=self.topBorder!+self.bounceHeight!
},完成日期:无
)
}
UIView.animateWithDuration(
0.5,
延迟:0,
选项:UIViewAnimationOptions.TransitionNone,
动画:{
self.dropDownMenu.frame.origin.y=self.topBorder!-self.dropdownHeight!
},完成:{uuuin
//self.delegate?.menuClosed?()
self.animating=false
self.dropDownMenu.hidden=true
self.ticket.setNeedsDisplay()文件
self.setNeedsDisplay()
}
)
删除=错误
}
func initSubViews(){
self.topBorder=self.frame.height*6/150
self.bottomBorder=self.frame.height-self.topBorder!
自弹高度=250
自降高度=350
containerView=UIView()
containerView.frame=CGRect(x:0,y:topBorder!,宽度:self.bounds.width,高度:dropdownHeight!+弹跳高度!)
containerView.clipsToBounds=true
票证=Tackticket()
ticket.frame=self.bounds
ticket.addTarget(self,action:#选择器(YellowTackleTicketComplete.ticketTapped),用于控制事件:。触地)
dropDownMenu=YellowDrawer()
dropDownMenu.frame=CGRect(x:0,y:topBorder!-dropdownHeight!,宽度:containerView.bounds.width,高度:dropdownHeight!)
dropDownMenu.hidden=true
dropDownMenu.backgroundColor=UIColor.clearColor()
addObserver(self,forKeyPath:“frame”,选项:。新建,上下文:nil)
dropDownBackground=黄色抽屉背景()
dropDownBackground.frame=CGRectMake(0,0,self.bounds.width,self.dropDown
cell.addSubview(ticket)
cell.contentView.addSubview(ticket)