不从xib调用协议UITableViewDataSource - Swift

时间:2017-07-21 19:38:01

标签: swift uitableview xib nib

当我运行APP时,我不知道为什么我的协议UITableViewDataSource没有执行。

这是我的xib设计: enter image description here

我从xib开始的课程是:CustomSearchDestination

 @IBOutlet weak var searchText: UITextField!
@IBOutlet weak var tableView: UITableView!

let xibName = "CustomSearchDestination"
var destinationListLocal: [Destinos] = []
var SearchViewControllerView: SearchViewController?

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
    xibSetup(xibName)

    SearchViewControllerView = SearchViewController()
    self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    searchText.delegate = self
    tableView.delegate = self
    tableView.dataSource = self
}

override init(frame: CGRect) {
    super.init(frame : frame)
    xibSetup(xibName)
}

此类内部已实施协议UITableViewDelegateUITableViewDataSource

extension CustomSearchDestination: UITableViewDelegate {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print("El count es: \(destinationListLocal.count)")
    return destinationListLocal.count
}

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let selectedCell: UITableViewCell = tableView.cellForRow(at: indexPath)!
    searchText.text = selectedCell.textLabel!.text
}}

extension CustomSearchDestination: UITableViewDataSource {

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
    let index = indexPath.row as Int

    if destinationListLocal.count == 0 {
        if let str = destinationListLocal[index].desDestino {
            cell.textLabel?.text = str
        }
    }

    return cell
}}

有人能指出我的错误吗?

编辑:我的层次结构是:

我有一个xib,我把这样的其他xib: enter image description here

这是我的班级CustomViewHandler。所以..我的类CustomSearchDestination是superClass CustomViewHandler的一个类。 同时是一类CustomSearch

class CustomViewHandler : UIView {
var view: UIView!

var controllerView : UIView!

// Keyboard
var activeField : UITextField?
var isKeyboardVisible = false
var fixValue : CGFloat?
var keyboardSize : CGFloat = 280


fileprivate func loadViewFromNib(_ nibName: String) -> UIView {
    let bundle = Bundle(for: type(of: self))
    let nib = UINib(nibName: nibName, bundle: bundle)

    // Assumes UIView is top level and only object in CustomView.xib file
    let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
    return view
}

func xibSetup(_ nibName: String) {
    view = loadViewFromNib(nibName)

    // use bounds not frame or it'll be offset
    view.frame = bounds

    // Makes the view stretch with containing view
    view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
    addSubview(view)

}}

0 个答案:

没有答案