簡體   English   中英

如何將UITableView添加到UIView?

[英]How do I add a UITableView to a UIView?

我有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)

這是您問題的完整代碼

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()
    }
}

tableView是xiOut文件中的IBOutlet嗎? 為什么需要addSubView?

暫無
暫無

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

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