Is there a way to add a UITableView
(not a Table View Controller
, but a TableView within another view) and set its content to "Dynamic Prototype"
in an XIB file
?
This works fine when adding a UITableView
in a view controller within the storyboard
. However, when I try to do the same in an XIB file, I cannot set its content to "Dynamic Prototype"
.
Update
In thinking about this recently, there is a fairly straightforward solution to the problem, which is to use the container view pattern.
uitableviewcontroller
onto the IB canvas. uitableviewcontroller
and select 'Embed' from the available segues types. uitableviewcontroller
to a separate storyboard/xib.Note - The table view controller will be accessible as a child view controller of the view controller that holds the container view in code.
Why is this the correct pattern? 1 Storyboard, 1 Controller. The TableView has it's own controller because it is complex enough to warrant a separate controller. Using the container view pattern allows you to maintain a single responsibility pattern. Avoids controller bloat.
Workaround for original answer
Please be aware of a gotcha where if you resize the tableview; the xml will be regenerated by Xcode and require you to re-add the attribute. I have confirmed that this edit still works in version 9.3+. A sample stub is available at https://github.com/mingsai/test-stub-tableviewdynamic-inxib.git.
Original Answer
tableview
tag locate the style attributedataMode="prototypes"
Not 100% sure on this, but I don't think there is a way to set the UITableView content to "Dynamic Prototype" in a XIB file.
However you can achieve the same functionality by creating another XIB file, called something like "MyTableViewCell.xib", that only contains a UITableViewCell, give the cell an identifier, go to File's Owner and in the identity inspector set it to the same view controller class as your table view xib, then create an IBOutlet in your view controller like this:
@property (nonatomic, assign) IBOutlet UITableViewCell *customCell;
then in the XIB, click on the File's Owner and control-drag to the uitableviewcell and set the cell's outlet to the "customCell" property.
(This might be done a lot easier if you find the button that looks like a play button inside a circle in the bottom left hand corner of the graphic editor, click on it, then do your dragging in that column).
After all that, 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 that makes sense and suits your needs!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With