简体   繁体   English

带有TableView的子视图控制器笔尖

[英]Child View Controller Nib with TableView

This is my first time using nib files, and I am using them because I was to toggle two views without bloating the storyboard for easy UI editing. 这是我第一次使用nib文件,我之所以使用它们是因为我要切换两个视图而不会膨胀情节提要以便于UI编辑。 I went with child view controller's so I could separate everything. 我使用了子视图控制器,所以我可以将所有内容分开。 One of the child controller's will contain table view delegate and data source. 子控制器的其中之一将包含表视图委托和数据源。 So I created a nib with a single view in IB. 因此,我在IB中创建了一个具有单个视图的笔尖。 I dragged in a table view, but it is already doing something odd. 我在表格视图中拖动,但是它已经做了一些奇怪的事情。

Here is what a table view dragged into a storyboard view controller looks like: 这是拖动到情节提要视图控制器中的表视图的样子:

普通情节提要tableview

And here is what it looks like in the nib file: 这是nib文件中的样子:

笔尖tableview

It still has the default California data in it. 它仍然具有默认的加利福尼亚数据。 Why doesn't it say "Table View Prototype"? 为什么不显示“表格视图原型”?

I tried to add a cell to it anyway but I couldn't. 我仍然尝试向其中添加一个单元,但是没有。 The first image is what normally happens, the cell is nested. 第一个图像是通常发生的情况,单元是嵌套的。 In the nib, though, it won't go into the table view. 但是,在笔尖中,它不会进入表格视图。

正常细胞插入笔尖细胞插入

I want to make the table view with custom cells, but I don't know what is wrong or if I am missing something since this is my first time using nibs. 我想使用自定义单元格创建表格视图,但是我不知道这是什么问题,或者我是否缺少某些东西,因为这是我第一次使用笔尖。

It still has the default California data in it. 它仍然具有默认的加利福尼亚数据。 Why doesn't it say "Table View Prototype"? 为什么不显示“表格视图原型”?

Because prototypes are a feature of storyboards only - not nibs. 因为原型仅是情节提要的功能,而不是笔尖。

I tried to add a cell to it anyway but I couldn't. 我仍然尝试向其中添加一个单元,但是没有。 The first image is what normally happens, the cell is nested. 第一个图像是通常发生的情况,单元是嵌套的。 In the nib, though, it won't go into the table view. 但是,在笔尖中,它不会进入表格视图。

To use a nib-designed cell type with a table view that doesn't come from a storyboard, you must have a separate nib for just the cell (there must be nothing else in the nib ). 要使用不是来自情节提要的表格视图使用笔尖设计的单元格类型,您必须为该单元格 单独拥有一个笔尖(笔尖中必须没有其他东西 )。 Register the nib with the table view (in code); 在表视图中注册笔尖(使用代码); from then on, when you dequeue a cell, the table view will load the cell from the nib. 从那时起,当您将一个单元出队时,表格视图将从笔尖加载该单元。 See this section of my book: 请参阅我的书的这一部分:

http://www.apeth.com/iOSBook/ch21.html#_custom_cells http://www.apeth.com/iOSBook/ch21.html#_custom_cells

It's normal that you cannot nest a Table View Cell inside a Table View in a xib file. 通常无法在xib文件的表视图中嵌套表视图单元格。 One possible approach to custom cells with xib files is to create a subclass of UITableViewCell and set it as the file owner of your custom cell in the xib file (create a xib file for the cell). 使用xib文件定制单元格的一种可能方法是创建UITableViewCell的子类,并将其设置为xib文件中定制单元格的文件所有者(为该单元格创建一个xib文件)。 Then you will be able to create outlets between UI elements in your custom cell and your custom UITableViewCell subclass. 然后,您将能够在自定义单元格的UI元素和自定义UITableViewCell子类之间创建出口。

In the following example, I'll use MyCustomCell as the name of the xib file containing the cell, and also the name of the custom class derived from UITableViewCell . 在以下示例中,我将使用MyCustomCell作为包含该单元格的xib文件的名称,以及从UITableViewCell派生的自定义类的名称。

To register the cell with your table view, put this in your UITableViewController subclass: 要将单元格注册到表格视图中,请将其放入UITableViewController子类中:

- (void)viewDidLoad 
{
    [super viewDidLoad];

    UINib *cellNib = [UINib nibWithNibName:@"MyCustomCell" bundle:nil];
    [self.tableView registerNib:cellNib forCellReuseIdentifier:@"MyCustomIdentifier"];
    ...
}

Finally, in your tableView:cellForRowAtIndexPath: method you can dequeue a custom cell using its identifier and set its visual properties using the outlets you created before: 最后,在tableView:cellForRowAtIndexPath:方法中,您可以使用其标识符使自定义单元出队,并使用之前创建的出口来设置其可视属性:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCustomIdentifier"];

    // Configure cell using its outlets

    return cell;
} 

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

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