1

I've created a UITableView that is entirely composed of four different groupings. Each section/group has a different number of rows. I've statically added the necessary number of rows and textfields for the bottom three groups. However, I do not know how many rows I will need for the top section/group. I've set up the top group as just having one cell in the .xib, but each time the view loads I am going to have to check how many cells I need and then dynamically add the necessary number of rows to the top section.

I've looked around StackOverflow and have seen people discussing the insertRowsAtIndexPaths methods, but I haven't been able to get it to work.

[settingsPageTableView beginUpdates];
[settingsPageTableView insertRowsAtIndexPaths:tempArray withRowAnimation:UITableViewRowAnimationAutomatic];
[settingsPageTableView endUpdates];

Do I need to put some specific code inside of the insertRowsAtIndexPaths methods? I've passed in an NSMutableArray as so:

 NSIndexPath *indexPath0 = [NSIndexPath indexPathForRow:1 inSection:0];
    NSMutableArray *tempArray=[NSMutableArray array];
   [tempArray addObject:indexPath0];

If anyone has any knowledge of how to combine static and dynamic cells in one UITableView, it would be extremely appreciated! For anyone that is familiar with the bluetooth page from the general settings on the iPhone, this is exactly what I'm looking to do. It has a variable number of devices show up as well as a static cell containing a UISwitch at the top. Does anyone know how to accomplish this?

Thanks a bunch!

Tommy Devoy
  • 13,441
  • 3
  • 48
  • 75

3 Answers3

3

I think you should do it all dynamically, just because the cell you describe on the bluetooth-page is always there doesn't mean that it's a static cell, just always created dynamically.

phillm
  • 166
  • 2
  • 9
2

So here's the workaround I just figured out. I know for sure there will never be more than 7 cells that I would ever need for that top portion of the UITable View. So then, I am able to just set up those 7 statically in the .xib file. From there I am able to only show the specific number that I need using this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


    switch(section) {
        case 0:  
            //Do a bunch of checking here on how many i actually need. Then, I will just return the required amount. As long as it is less than the number of cells I have statically placed in my .xib, everything works good.
            return 4;
        case 1:
            return 3;
        case 2:
            return 1;
        case 3:
            return 2;
        case 4:
            return 3;
        default:
            return 0;
        }
//Every case but the first always needs the same amount of cells, so those are no problem.
}
Tommy Devoy
  • 13,441
  • 3
  • 48
  • 75
1

Best and Easiest Way:

1) Place Container View in the Header of a Dynamic TableView 2) Assign the Static TableView to this Container and Untick the "Scrolling Enabled"

Mazen Kasser
  • 3,559
  • 2
  • 25
  • 35