简体   繁体   中英

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. 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. 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:

笔尖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

It's normal that you cannot nest a Table View Cell inside a Table View in a xib file. 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). Then you will be able to create outlets between UI elements in your custom cell and your custom UITableViewCell subclass.

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 .

To register the cell with your table view, put this in your UITableViewController subclass:

- (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:

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

    // Configure cell using its outlets

    return cell;
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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