[英]Setting delegate and datasource in custom UITableView
我正在嘗試創建UITableView
作為UIViewController
的自定義子視圖,但是數據未顯示在表中。 我懷疑問題出在表的datasource
和我的delegate
沒有設置。 如何鏈接它們? UITableViewDataSource
似乎在UITableView
類中不起作用?
的UIViewController
let projectDeck = ProjectDeck()
projectDeck.frame = CGRect(x: 0, y: statusBarHeight + 132, width: screenWidth, height: screenHeight - 132)
self.view.addSubview(projectDeck)
UITableView的
class ProjectDeck: UITableView {
var items = ["Test", "TestTest", "TestBestWestVest"]
override func numberOfRows(inSection section: Int) -> Int {
return items.count
}
override func cellForRow(at indexPath: IndexPath) -> UITableViewCell? {
let cell = UITableViewCell()
cell.textLabel?.text = items[indexPath.row]
return cell
}
}
在viewcontrloller中設置委托和數據源
即在viewcontroller
projectdesc.tableview.datasource = self
projectdesc.tableview.delegate = self
並在viewccontroller中實現像cellforRow這樣的tableview方法
並確保您正確地將tableview作為子視圖添加到viewcontroller中
您需要添加數據源和委托引用。
您可以從插座或情節提要中進行操作。
使用插座:
yourTableView.datasource = self
yourTableView. delegate = self
使用StoryBoard
ctrl將表視圖拖動到第一個響應者旁邊的viewcontroller上,然后單擊數據源和委托。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.