【发布时间】:2014-08-19 14:54:29
【问题描述】:
我创建了一个自定义类来为我的一些 MainTableViewControllers 添加一个侧边栏。如果用户单击 MainTableRow 侧边栏应该打开并在另一个 UITableView 中显示一些详细信息。所以我需要在我的侧边栏视图中创建一个 UITableView。这个新表格应该有一些不同的自定义单元格。
我的问题是,我的表中没有显示数据。单元格始终为空。我是否需要在 init 上设置任何其他内容来告诉 TableViewController 他在哪里找到他的 UITableViewDataSource?
任何帮助将不胜感激。
这是我的自动取款机:
class SideView: UIView, UITableViewDataSource, UITableViewDelegate {
在初始化时,我将 TableView 加载到我的视图中:
init(view: UIView) {
super.init(frame: CGRect(x: view.frame.size.width, y: 0, width: 150, height: view.frame.size.height-40))
var tvc:UITableView = UITableView(frame: CGRectMake(0, 0, 150, 768), style: UITableViewStyle.Plain)
addSubview(tvc)
....
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("TitleLabelCell", forIndexPath: indexPath) as SideLabelTitleTableViewCell
cell.titleText = "Hello Test"
return cell
....
这是我的自定义单元自动取款机:
var titleText:String!
init(style: UITableViewCellStyle, reuseIdentifier: String) {
super.init(style: style, reuseIdentifier: "TitleLabelCell")
// Initialization code
var titleLabel:UILabel!
titleLabel.frame = CGRectMake(0,0,150,44)
titleLabel.text = titleText
self.contentView.addSubview(titleLabel)
}
编辑:好的,现在数据源应该可以工作了,但是我无法使用我的 UITableViewCell。
我使用:
tvc.dataSource = self
但是当我尝试从 sideViewClass 为“titleLabel”设置一个值时,它始终为“nil”。如果我从我的 CustomCellClass 设置它,它会按预期工作:
init(style: UITableViewCellStyle, reuseIdentifier: String) {
super.init(style: style, reuseIdentifier: "TitleLabelCell")
var labelText:String!
println(self.labelText)
var titleLabel:UILabel = UILabel(frame: CGRectMake(0,0,150,44))
titleLabel.text = self.labelText // THIS IS NOT WORKING, always nil?
self.contentView.addSubview(titleLabel)
并在 cellForRowAtIndexPath 上设置我的单元格值:
cell.labelText = "Test"
【问题讨论】:
标签: ios uitableview swift