【问题标题】:Add UITableView to Sidebar programmatically以编程方式将 UITableView 添加到侧边栏
【发布时间】: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


    【解决方案1】:

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

    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
    

    【讨论】:

      【解决方案2】:

      尝试以正确的语法实现类:

      class MyCell: UITableViewCell {
         var titleLabel = UILabel(frame:CGRectMake(0.0, 0.0, 150.0, 44.0))
         var labelText: String! {
             willSet {
                 titleLabel.text = newValue
             }
         }
         init(style: UITableViewCellStyle, reuseIdentifier: String) {
             super.init(style: style, reuseIdentifier: "TitleLabelCell")
             contentView.addSubview(textLabel)
         }
      }
      

      不要在方法中声明外部 var/let

      还有一件事,如果您要使用 XIB/storyboard,请使用 awakeFromNib() 而不是 init(style: UITableViewCellStyle, reuseIdentifier: String)

      【讨论】:

      • 谢谢。我已经这样做了,标签仍然显示。但我无法从 cellForRowAtIndexPath 为我的 titleLabel 设置值。总是得到“零”。你有什么想法吗?
      • 是的,你不应该使用 var labelText:String!,而是在 cellForRowAtIndexPath: 中使用 cell.titleLabel.text = someText
      • 或者你可以使用 var labelText:String { willSet { titleLabel.text = newValue } }
      • @derdida 我更新了我的答案,现在应该对你有用
      • 谢谢:但仍然得到“在展开可选值时意外发现 nil” - 如果我为我的标签创建 IBOutlet。 @IBOutlet var titleLabel:UILabel!并在初始化时使用: var titleLabel:UILabel = UILabel(frame: CGRectMake(0,0,150,44)) - 我还需要其他什么吗(通常我使用 Interface Builder 连接我的 Outlets,但这是以编程方式创建的)
      【解决方案3】:

      您需要设置委托和数据源,然后重新加载表。

      tvc.delegate = self
      tvc.dataSource = self
      tvc.reloadData()
      

      还将tableview单元格注册到表格中

      tvc.registerClass(SideLabelTitleTableViewCell, forCellReuseIdentifier:"TitleLabelCell")
      

      【讨论】:

      • 谢谢。我编辑了我的问题。为单元格中的标签设置值仍然存在问题。我真的需要重新加载吗(因为我在 ViewDidLoad 上设置了它,所以通常它应该可以在不重新加载的情况下工作,或者?)
      • 重新加载确保表格在第一次设置时被填满。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-20
      • 1970-01-01
      • 1970-01-01
      • 2014-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多