Add UITableView with dynamic content in XIB file

Is there a way to add a UITableView (not a Table View Controller , but a TableView to another view ) and set its contents to "Dynamic Prototype" in the XIB file ?

This works great when adding a UITableView to the view controller in the storyboard . However, when I try to do the same in a XIB file, I cannot set its contents to "Dynamic Prototype" .

+7
source share
2 answers

Not 100% sure about this, but I don’t think there is a way to set the contents of the UITableView to a “Dynamic Prototype” in the XIB file.

However, you can achieve the same functionality by creating another XIB file called "MyTableViewCell.xib" that contains only UITableViewCell, assigns an identifier to the cell, goes to the file owner, and the same view controller class is set in the identity inspector as xib, and create an IBOutlet in your view controller as follows:

  @property (nonatomic, assign) IBOutlet UITableViewCell *customCell; 

then in the XIB click “File Owner” and drag the control into the uitableviewcell cell and set the cell output to the “customCell” property.

(This can be made much simpler if you find a button that looks like a play button inside a circle in the lower left corner of the image editor, click on it, and then drag it to this column).

After all this, in cellForRowAtIndexPath, use code similar to this:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CustomCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:self options:nil]; cell = customCell; } // Other cell configurations.... } 

Hope this makes sense and fits your needs!

+2
source
  • Open file navigator
  • Right-click YourCustomView.xib
  • Open As> Source Code

openas

  1. Find the beginning of XML with

    tableview-xml

    1. In the tableview XML tableview find the style attribute
    2. Leaving a blank space in front of the style tag, insert it dataMode="prototypes"
+2
source

All Articles