我有2个xib文件:自定义UIView和自定义单元xib。 我也有一个仅包含UILabel的Custom Cell类。 名字都是正确的。
UITableView的委托已连接到视图。
我需要将UITableView添加到UIView。从教程中,我读到了这是添加方法。但是由于某种原因,这行不通,我在此代码中收到两种错误,即我需要注册单元格nib或在展开可选内容时发现nil。我究竟做错了什么?请帮忙!
private let myArray: NSArray = ["First","Second","Third"]
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Num: \(indexPath.row)")
print("Value: \(myArray[indexPath.row])")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath)
cell.textLabel!.text = "\(myArray[indexPath.row])"
return cell
}
@IBOutlet var contentView: UIView!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var HelperLabel: UILabel!
override init(frame: CGRect){
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit(){
let nib = UINib(nibName: "MyCell", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "MyCell")
tableView.dataSource = self
tableView.delegate = self
self.addSubview(tableView)
答案 0 :(得分:-1)
这是您问题的完整代码
CustomCell类的代码:为单元格添加Custom Xib
import UIKit
class CustomCell: UITableViewCell {
@IBOutlet weak var myLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
CustomView类的代码:将Custom Xib用于视图
import UIKit
class CustomView: UIView {
var tableData: [String] = ["First","Second","Third"]
@IBOutlet weak var myTableView: UITableView!
class func instanceFromNib() -> UIView {
return UINib(nibName: "CustomView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
static func nibInstance() -> CustomView? {
let customView = UINib(nibName: "CustomView", bundle: nil).instantiate(withOwner: nil, options: nil).first as? CustomView
return customView
}
func baseInit() {
self.myTableView.delegate = self
self.myTableView.dataSource = self
self.myTableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCell")
}
}
extension CustomView : UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.myLabel.text = tableData[indexPath.row]
return cell
}
}
注意:在我的情况下,将Xib CustomClass设置为UIView Class都是“ CustomView”
控制器代码:
class CustomViewController: UIViewController {
var customView : CustomView?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
DispatchQueue.main.asyncAfter(deadline: DispatchTime(uptimeNanoseconds: UInt64(0.5))) {
self.loadViewCustom()
}
}
func loadViewCustom() {
self.customView = CustomView.nibInstance()
self.customView?.frame = self.view.frame
self.customView?.baseInit()
self.view.addSubview(self.customView!)
self.customView?.myTableView.reloadData()
}
}
答案 1 :(得分:-2)
tableView是ibOut文件中的IBOutlet吗?为什么需要addSubView?