我试图创建一个UITableView
作为我的UIViewController
的自定义子视图,但数据未显示在表中。我怀疑问题是我的delegate
和datasource
表未设置。我如何链接它们? UITableViewDataSource
似乎在UITableView
类中不起作用?在自定义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
}
}
projectDeck.delegate = self,与数据源相同(如果您的UIViewController是委托或数据源)**请记住**,您需要为您的tableView单元格提供正确的高度。 https://developer.apple.com/reference/uikit/uitableviewdelegate/1614998-tableview – 2017-03-01 04:40:05
[在Swift中以编程方式设置uitableview的委托和数据源的可能的重复](http://stackoverflow.com/questions/27022136/set-delegate -and-datasource-of-uitableview-programmatically-in-swift) – 2017-03-01 04:42:26
如何使单元出列:http://stackoverflow.com/questions/24471954/how-to-use-dequeuereusablecellwithidentifier-in-swift – 2017-03-01 04:43:58