简体   繁体   中英

How to create a custom view storyboard outlet from a custom tableView cell?

I am trying to create a custom view in a custom tableviewcell. But I couldn't see any results.

//-- This is the custom tableview cell where I created custom view outlet and mentioned custom view class when creating outlet from tableviewcell.

class NetworkAccessByLocationTableViewCell: UITableViewCell {
    
    @IBOutlet weak var chartView: MyCustomView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
 }

//--This is my custom View where I have code customized for view.


class MyCustomView: UIView {

    override init(frame: CGRect) {
           super.init(frame: frame)
           setup()
       }

       required init?(coder aDecoder: NSCoder) {
           super.init(coder: aDecoder)
           setup()
       }
    
        func setup() {
        //I have something here
        }

        func createBezierPath() -> UIBezierPath {
         //I have some code here
        }

}

I don't get what your exact problem is but in order to use a custom UIView that is accessible in interface Builder, you need to create a IBDesignable view. Also since your's custom view doesn't contain xib, why don't you simply create an instance of your custom view in your cell and add anchor to it programmatically

let chartView = MyCustomView()
self.addSubview(chartView)

Then give the anchor your want to give in the custom cell for your chartView.

Try this code

class MyCustomView: UIView {

override init(frame: CGRect) {
       super.init(frame: frame)
       setupView()
       
   }

   required init?(coder aDecoder: NSCoder) {
       super.init(coder: aDecoder)
       setupView()
    
   }

   private func setupView() {
        let bundle = Bundle(for: MyCustomView.self)
        if let view = UINib(nibName: <Name of MyCustomView's Xib file>, bundle: bundle).instantiate(withOwner: self, options: nil).first as? UIView {
            view.frame = bounds
            view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            view.translatesAutoresizingMaskIntoConstraints = true
            addSubview(view)
        }
        setup()
    }

    func setup() {
    //I have something here
    }

    func createBezierPath() -> UIBezierPath {
     //I have some code here
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM