Is there a way to combine static tableview cells (static content) with dynamic tableview cells (prototype content) using storyboard?
To do this go to Interface builder and click on the Table View. Now you need to set the content type of the table view to Static Cells.
We also need to change the class in Interface builder for the table view controller to be of type ViewController. To do this go to Interface builder and click on the Table View. Now you need to set the content type of the table view to Static Cells.
The biggest difference when using a static table view is that you need to do it through Interface builder, and it needs to use a UITableViewController. You cannot add a normal UITableView in a UIViewController. Now that we have the basics covered we can start implementing the static table view with custom cells.
The developer has control over the way rows and sections set up. There are two types of Table views: Static and dynamic. Static table views have a specific number of table sections and a specific number of rows in each section set at design time. They are easy to set in the storyboard.
I suggest you treat your table as dynamic, but include the cells you always want at the top. In the Storyboard, place a UITableViewController
and have it use a dynamic table. Add as many UITableViewCell
prototypes to the table as you need. Say, one each for your static cells, and one to represent the variable cells.
In your UITableViewDataSource
class:
#define NUMBER_OF_STATIC_CELLS 3 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.dynamicModel count] + NUMBER_OF_STATIC_CELLS; }
and, then
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row < NUMBER_OF_STATIC_CELLS) { // dequeue and configure my static cell for indexPath.row NSString *cellIdentifier = ... // id for one of my static cells } else { // normal dynamic logic here NSString *cellIdentifier = @"DynamicCellID" // dequeue and configure for [self.myDynamicModel objectAtIndex:indexPath.row] } }
I had a problem, although it was a slight variant of this. I actually wanted to mix dynamic and static cells but in different groups. Meaning group 1 would have static only cells and group 2 would have dynamic cells.
I accomplished this by actually hard coding static cell values (based on their prototype cell identifiers). The dynamic sections would have normal dynamically populated content. Here is some example code in case anyone else has the same issue:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { if (section == 1){ return @"Dynamic Cells"; } if (section == 0){ return @"Static Cells"; } } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 0) { return 1; //However many static cells you want } else { return [_yourArray count]; } } -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { if (indexPath.section == 0) { NSString *cellIdentifier = @"staticCellType"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } cell.textLabel.text = @"some static content"; return cell; } else if (indexPath.section == 1){ NSString *cellIdentifier = @"dynamicCellType"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } cell.textLabel.text = [_yourArray objectAtIndex:indexPath.row]; return cell; } return nil; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; }
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