我想创建一个自定义视图。所以我像这样设计视图。它称为TagResolutionView.xib
This is how I designed my view
并且该文件的TagResolutionView.swift
文件看起来像这样
import UIKit
@IBDesignable class TagResolutionView: UIView {
@IBOutlet var tagResolutionView: UIView!
@IBOutlet var tableview: UITableView!
required init?(coder: NSCoder) {
super.init(coder: coder)
tableview = UITableView()
}
override init(frame: CGRect) {
tableview = UITableView()
super.init(frame: frame)
}
override func awakeFromNib() {
super.awakeFromNib()
}
@IBOutlet weak var delegate: UITableViewDelegate? {
get {
return tableview.delegate
}
set {
tableview.delegate = newValue
}
}
@IBOutlet weak var dataSource: UITableViewDataSource? {
get {
return tableview.dataSource
}
set {
tableview.dataSource = newValue
}
}
func registerClass(cellClass: AnyClass?, forCellReuseIdentifier identifier: String) {
tableview.register(cellClass, forCellReuseIdentifier: "cellClass")
}
func dequeueReusableCellWithIdentifier(identifier: String) -> UITableViewCell? {
return tableview.dequeueReusableCell(withIdentifier: identifier)
}
override func layoutSubviews() {
super.layoutSubviews()
tableview.frame = self.bounds
self.addSubview(tableview)
}
}
这是我在希望显示自定义视图的主视图控制器中插入视图的方式。
这是我在视图控制器中对其进行配置的方式。
在viewDidLoad
..
stdProcedViewHeight.constant = 533 //Since the view I took above (as given in the pic) is less than the height of the actual TagResolutionView.xib, I increased its height here programatically.
standardsProceduresView.delegate = self
standardsProceduresView.dataSource = self
standardsProceduresView.registerClass(cellClass: UpdatingListTableViewCell.self, forCellReuseIdentifier: "cellClass") //I have also made .xib & .swift files for UpdatingListTableViewCell
也添加了这些方法。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell: UpdatingListTableViewCell = self.standardsProceduresView.dequeueReusableCellWithIdentifier(identifier: "cellClass") as! UpdatingListTableViewCell // cell.nameLbl.text = "MyName" return cell } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 10 }
但是我无法通过所有这些操作获得所需的输出...我得到的只是设置高度(此处为533的高度)的空表视图,而设计没有其他内容...
我想创建一个自定义视图。所以我像这样设计我的视图。它叫做TagResolutionView.xib。这就是我设计视图的方式。为此,TagResolutionView.swift文件看起来像这样……
之所以无法显示表视图,是因为您通过在代码中调用tableView = UITableView()
来重置其所有内容。