【问题标题】:How do I add a UITableView to a UIView?如何将 UITableView 添加到 UIView?
【发布时间】:2019-05-09 00:05:09
【问题描述】:

我有 2 个 xib 文件:自定义 UIView 和自定义单元 xib。 我还有一个只包含 UILabel 的自定义单元类。 名字都是正确的。

UITableView 的代理连接到视图。

我需要将 UITableView 添加到 UIView。从我读过的教程中,这是如何添加它的。但是由于某种原因,这不起作用,我得到了 2 种类型的错误代码,说我需要注册单元笔尖,或者在展开可选时它发现 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)

【问题讨论】:

  • 您是否为创建的单元格创建了自定义类?
  • 是的。我通过故事板将它连接到自定义 Xib。

标签: ios uitableview uiview uiviewcontroller swift4


【解决方案1】:

这是您问题的完整代码

CustomCell 类的代码:为 Cell 获取自定义 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 类的代码:使用自定义 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()
    }
}

【讨论】:

  • 理解有什么问题请告诉我
【解决方案2】:

tableView 是 xib 文件中的 IBOutlet 吗?为什么需要 addSubView?

【讨论】:

  • 你应该说在 cmets 部分这不是答案。
猜你喜欢
  • 2012-03-05
  • 1970-01-01
  • 2016-06-19
  • 2016-10-29
  • 2016-06-27
  • 1970-01-01
  • 1970-01-01
  • 2014-08-23
  • 2023-03-21
相关资源
最近更新 更多