Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine static and prototype content in a table view

Is there a way to combine static tableview cells (static content) with dynamic tableview cells (prototype content) using storyboard?

like image 313
tazboy Avatar asked Feb 17 '12 04:02

tazboy


People also ask

How to create a static cell in a table view?

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.

How to set content type of Table View in Interface Builder?

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.

What is the difference between UIViewController and static table view?

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.

What is the difference between static and dynamic table views?

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.


2 Answers

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]     } } 
like image 174
danh Avatar answered Oct 02 '22 13:10

danh


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; } 
like image 27
AbuZubair Avatar answered Oct 02 '22 13:10

AbuZubair