2017-02-01 108 views
1

我有一個問題時,把UITableViewUIViewUITableView裏面UIView在Swift 3.0

我設置了一個代表數據源並實現所有方法。

這是我的UIView類。

@objc class ChooserView: UIView, UITableViewDelegate, UITableViewDataSource { 


@IBOutlet weak var backgroundView: UIView! 
@IBOutlet weak var showView: UIView! 
@IBOutlet weak var tableView: UITableView!  

public class func createChooserView() -> ChooserView { 

    var view: ChooserView! 
    view = Bundle.main.loadNibNamed(String(describing: ChooserView.self), owner: self, options: nil)?.first as! ChooserView 

    _ = MLAutolayouts.fillContainer(UIApplication.shared.keyWindow!.subviews.first!, view: view) 

    //Register cell nib 
    view.tableView.register(UINib(nibName: "PhoneCell", bundle: nil), forCellReuseIdentifier: "PhoneCell") 

    return view 
    } 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
    } 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: PhoneCell.self)) as! PhoneCell 

    return cell 
    } 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    print("TAP...") 
    } 
} 

這是outles

enter image description here

當運行我的應用程序。我得到這個錯誤

-[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7fded2e772d0 

Log img。

enter image description here

任何想法...

感謝。

+1

你宣佈你在代碼中的類,但看起來你忘了聲明在您的視圖IB有類的類型,這就是爲什麼你看到' - [UIView tableView:numberOfRowsInSection:]'而不是' - [ChooserView ...' – Ossir

+0

如何連接UIView? @Hasham –

回答

2

將IB的dataSource和委託連接到ChooserView而不是任何其他視圖。

你的表視圖和 只需右擊按照附着的gif的正確連接IB:

enter image description here

+0

它的工作原理。謝謝 –

0

首先,你已經應用了故事板的插座,並且你也在代碼中指定了插件。沒有必要這樣做。其次,包含標籤或按鈕的表有任何自定義類嗎?如果你錯過了與故事板的任何IBoutlet連接或指定的2連接用於同一個插座檢查一次。

+0

嗨josueh以類似的方式連接你的uiview作爲你的表視圖。 –