简体   繁体   English

从Storyboard而非nib加载自定义TableViewCell

[英]Loading a custom TableViewCell from Storyboard instead of nib

I am new to StackOverflow , and have been struggling with this issue for a while, really hoping someone out there can help and point me in the right direction. 我是StackOverflow新手,并且在这个问题上苦苦挣扎了一段时间,真的希望有人可以帮助您,并为我指明正确的方向。

I am trying to make a custom UITableView and a custom UITableViewCell - they will both go together to form a custom UITableView which I would like to reuse across multiple projects. 我正在尝试制作一个自定义UITableView和一个自定义UITableViewCell它们都将一起形成一个自定义UITableView ,我想在多个项目中重复使用。 So what I am trying to do is encapsulate all row + keyboard handling etc in the custom UITableView , and the cell's look in the custom UITableViewCell . 所以我想做的是将所有行+键盘处理等封装在自定义UITableView ,并将单元格的外观封装在自定义UITableViewCell

In a new StoryBoard, I added a UIViewController and added a UITableView , designed how I wanted the table to look and added a prototype cell to it. 在新的StoryBoard中,我添加了UIViewController并添加了UITableView ,设计了我希望表如何显示的方式,并向其中添加了原型单元。 I designed this with the appropriate labels, fields and constraints. 我设计了适当的标签,字段和约束。 Lets call this custom look table "MyCustomTableWithCustomCell" 让我们将此自定义外观表称为“ MyCustomTableWithCustomCell”

So now I have my custom table and what I want is to reuse this across other Storyboards. 所以现在有了我的自定义表,我想要在其他Storyboard中重用它。

I have found one solution (thanks stackoverflow!), but it isn't exactly what I wanted: 我找到了一个解决方案(感谢stackoverflow!),但这并不是我想要的:

  • I extracted the prototype cell from MyCustomTableWithCustomCell (cut and paste into an Empty Interface Builder document) and saved it as a xib. 我从MyCustomTableWithCustomCell提取了原型单元(剪切并粘贴到Empty Interface Builder文档中)并将其保存为xib。
  • I then use this inside my custom UITableView via registerNib() 然后,我通过registerNib()在自定义UITableView使用它
  • I can create a new UIViewController in any Storyboard, add a UITableView , set its class to MyCustomTableWithCustomCell and it will do what I want 我可以在任何Storyboard中创建一个新的UIViewController ,添加一个UITableView ,将其类设置为MyCustomTableWithCustomCell ,它将完成我想要的操作

The problem is, I don't like how the cell is in a separate xib file, as it can't be previewed easily across different device sizes via IB auto-preview. 问题是,我不喜欢该单元格在单独的xib文件中的方式,因为无法通过IB自动预览轻松地在不同设备大小之间预览它。 Regardless of which device size I select, it always looks the same. 无论我选择哪种设备大小,它始终看起来相同。 It is also hard to see how the design fits with the rest of the screen unless I build and preview in Simulator every time I want to change anything. 除非每次我想更改任何内容时都在Simulator中进行构建和预览,否则很难看到设计与屏幕的其余部分如何匹配。 I'd prefer if the cell design could remain as part of the Prototype TableViewCell inside the blueprint Storyboard. 我希望单元格设计可以保留在蓝图故事板上的Prototype TableViewCell的一部分。 Then I could easily modify it, preview across different device sizes etc. 然后,我可以轻松地对其进行修改,在不同设备尺寸之间进行预览等。

So my question is, what is the best way to do the same thing, but instead of using a xib file, using a Prototype Table Cell from a Storyboard as the blueprint. 所以我的问题是,做同一件事的最好方法是什么,但是使用情节提要中的原型表单元作为蓝图,而不是使用xib文件。 Or in other words, is there a way to extract the Prototype cell into a Nib at runtime so that I can register it when I create my custom table. 换句话说,有一种方法可以在运行时将原型单元格提取到Nib中,以便在创建自定义表时可以对其进行注册。

Eg. 例如。 something like: 就像是:

    var myTableCellNib = myStoryBoard.myViewController.myTableView.myPrototypeCell.getNib()
    registerNib(myTableCellNib)

Look forward to hearing your thoughts! 期待听到您的想法!

Make UITableViewCell Swift file - 制作UITableViewCell Swift文件-

import UIKit

    class ClassName: UITableViewCell {

    @IBOutlet weak var lblData: UILabel!


    override func awakeFromNib() {
        super.awakeFromNib()


    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

Go to storyboard - > View Controller -> Table View -> Drag and drop table view cell and design it, give class name as ClassName and identifier. 转到情节提要->视图控制器->表格视图->拖放表格视图单元格并进行设计,将类名称指定为ClassName和ID。

Go to cellForRowAtIndexPath 转到cellForRowAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    var cell : ClassName! = tableView.dequeueReusableCellWithIdentifier("identifier") as! ClassName
    cell.lblData.text = ""
    return cell as ClassName
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM