2017-02-10 51 views
3

試圖在XCode Playgrounds中使用表格來了解它是如何工作的,因爲有很多東西連接到UITableView,並且一些教程描述了這些關係和場景下的繼承(如UIView - >UIScrollView - >UITableView - >UITableViewCell)以及它如何管理繼承,連接和委派。通常,對於普通的導師,您將獲得代碼片段以及如何在XCode中建立委託連接以及調用正確方法的說明。結果 - 你記住了這些步驟,但仍然不理解表的機制。以下是我嘗試採用XCode Playgrounds繪製簡單表格的代碼片段。這個問題 - 我無法正確註冊程序化的表格單元格的重用標識符,從類或其外部。錯誤的性質在XCode Playgrounds中註冊單元重用標識符

原因:'無法使標識符單元出隊 - 必須爲標識符註冊一個nib或類或連接故事板中的原型單元格。

請幫忙指出什麼是錯的,爲什麼。

class TableViewController: UITableViewController { 

let tableData = ["Matthew", "Mark", "Luke", "John"] 

override func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return tableData.count 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell 
    cell.textLabel?.text = tableData[indexPath.row] 
    return cell 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.tableView.dataSource = self 
//Did this part as comments to try to do this outside the class 
//  self.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480) 
//  self.tableView = UITableView(frame:self.view.frame) 
//  self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") 
    self.view.addSubview(self.tableView) 

} 

} 
let frame = CGRect(x: 0, y: 0, width: 320, height: 480) 
let tableView = UITableView(frame: frame, style: .plain) 
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") 
PlaygroundPage.current.liveView = TableViewController() 

回答

1

在一個UIViewController contaning一個UITableView,編程註冊自定義的UITableViewCell子類的到的UITableView的一個實例是由實施:
- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier

這是registeringClass 應該在設置表dataSource之前完成。

+0

感謝您的回覆!這也是如此。但是爲什麼當我忽略設置表的'dataSource'時代碼正常工作。 UITableViewController默認使用self作爲表'dataSource'? –

+0

是的,當你繼承一個'UITableViewController'時,它執行(免費)設置數據和(抽頭)委託處理程序到'self','UITableViewController'的實例。 – ystack

+0

對於Swift 3,其語法是:'tableView.register(MyCustomTableViewCell.classForCoder(),forCellReuseIdentifier:「Cell」)''。這是寫在'loadView()'或'viewDidLoad()'中的'UITableViewController'裏面。 –

1

後很長一段時間從各種教程建築物表的不同的解決方案打我終於也陷入了UITableViewControllerUIViewController之間使用的一些差異用於顯示UITableView。在我的問題的例子中,一些代碼行只是多餘的。發生這種情況是因爲我試圖實現在UIViewController中構建表的方法,但我使用了UITableViewController,而且試圖設置dataSource和所有視圖屬性,但據我現在瞭解 - UITableViewController已經爲我做了所有這些工作(如果我錯了,請在這裏糾正)。的代碼的功能片段是以下:

class TableViewController: UITableViewController { 

let tableData = ["Matthew", "Mark", "Luke", "John"] 

    override func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return tableData.count 
    } 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell 
    cell.textLabel?.text = tableData[indexPath.row] 
    return cell 
    } 

    override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") 

    } 
} 

PlaygroundPage.current.liveView = TableViewController()