簡體   English   中英

如何從自定義 tableView 單元格創建自定義視圖 storyboard 插座?

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

我正在嘗試在自定義 tableviewcell 中創建自定義視圖。 但是我看不到任何結果。

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

}

我不明白您的確切問題是什么,但為了使用可在界面生成器中訪問的自定義 UIView,您需要創建一個 IBDesignable 視圖。 此外,由於您的自定義視圖不包含 xib,為什么不簡單地在單元格中創建自定義視圖的實例並以編程方式向其添加錨點

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

然后在您的 chartView 的自定義單元格中提供您想要提供的錨點。

試試這個代碼

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
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM