2017-04-10 15 views
1

無法導入UITableViewDataSource如果我使用一個自定義單元格BaseCell。只有UITableViewCell作品雖然BaseCell從類型UITableViewCell不能使用UITableViewDataSource與自定義單元格

import UIKit 

class XptoViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

@IBOutlet weak var table: UITableView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int { 
    return 10 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> BaseCell { 
    let cell = BaseCell() 
    return cell 
} 

}

+0

要返回的UITableViewCell沒有自定義類細胞。 –

+0

@TusharSharma粘貼錯誤的代碼;) – bruno

+1

你已經註冊的小區?爲什麼繞過出隊?數據源是否設置?代表組? – Larme

回答

4

嘗試相反:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = BaseCell() 
    return cell 
} 

但你可能想使用類似:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("BaseCell") as? BaseCell 
    return cell! 
} 

製作確定你設置「BaseCell」作爲你的手機標識符,如以下: Sample of interface Builder

如果你得到「而展開找到無可選」就像一個運行時錯誤,你需要看到BaseCell作爲類爲您的細胞,見圖片: enter image description here

相關問題