gpt4 book ai didi

ios - 以编程方式将 UITableView 添加到侧边栏

转载 作者:行者123 更新时间:2023-11-29 02:43:55 38 4
gpt4 key购买 nike

我创建了一个自定义类来向我的一些 MainTableViewController 添加侧边栏。如果用户单击 MainTableRow,侧边栏应该打开并在另一个 UITableView 中显示一些详细信息。所以我需要在我的侧边栏 View 中创建一个 UITableView 。这个新表应该有一些不同的自定义单元格。

我的问题是,我的表中没有显示数据。单元格始终是空的。我是否需要在 init 上设置其他任何内容来告诉 TableViewController 在哪里找到他的 UITableViewDataSource?

如有任何帮助,我们将不胜感激。

这是我的 atm 代码:​​

class SideView: UIView, UITableViewDataSource, UITableViewDelegate {

在初始化时,我将 TableView 加载到我的 View 中:

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"

最佳答案

将您的自定义单元格类别更改为:

var titleLabel:UILabel!
init(style: UITableViewCellStyle, reuseIdentifier: String) {
super.init(style: style, reuseIdentifier: "TitleLabelCell")
// Initialization code

titleLable = UILable()
titleLabel.frame = CGRectMake(0,0,150,44)

self.contentView.addSubview(titleLabel)
}

然后

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

var cell = tableView.dequeueReusableCellWithIdentifier("TitleLabelCell", forIndexPath: indexPath) as SideLabelTitleTableViewCell

cell.titleLabel.text = "Hello Test"

return cell

关于ios - 以编程方式将 UITableView 添加到侧边栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25386618/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com