swiftuitableviewuser-interface

UITableViewCell with Custom View


I am desperately trying to add a custom View to a UITableViewCell. I have a UITableViewController in Storyboard which is linked to a TestTableViewController Class and the prototype cell has the identifier "Cell2".

So this is the code in UITableViewController:

    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
}

The dataArray is just for testing purposes and has one item in it. So what I get is a Table View with one empty cell. In the XCode UI Debugger I can see the custom view. But its not shown in the simulator or on a device.

enter image description here

If anyone can help, thank you so much!

This is the code of the view:

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)
    }

}

Solution

  • You should use a designated initializer for your UIView subview. That is either init(frame:CGRect) or in add it in Interface Builder. You are also not passing let data = dataArray[indexPath.row] to your cell, not sure if you're aware of that. You also never call initSubViews() which seems to be a big part of your custom view.