【问题标题】:How to create a custom view storyboard outlet from a custom tableView cell?如何从自定义 tableView 单元格创建自定义视图故事板插座?
【发布时间】:2020-08-02 19:20:53
【问题描述】:

我正在尝试在自定义 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
        }

}

【问题讨论】:

  • 您能否更详细地描述您遇到的问题?
  • 当您说“看不到任何结果”时,是MyCustomView 没有显示还是您无法在NetworkAccessByLocationTableViewCell 上链接chartView IBOutlet。另外,您使用的是动态原型单元格吗?

标签: ios swift customization custom-view


【解决方案1】:

我不明白您的确切问题是什么,但为了使用可在界面生成器中访问的自定义 UIView,您需要创建一个 IBDesignable 视图。此外,由于您的自定义视图不包含 xib,为什么不简单地在单元格中创建自定义视图的实例并以编程方式向其添加锚点

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

然后在您的 chartView 的自定义单元格中给出您想要给出的锚点。

【讨论】:

    【解决方案2】:

    试试这个代码

    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
        }
    

    }

    【讨论】:

      猜你喜欢
      • 2012-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-19
      • 1970-01-01
      相关资源
      最近更新 更多