views:

299

answers:

2

My UITableViewCells take up the full viewing area. I'd like to use a grouped table so the cells appear to have rounded corners. Right now, if I set the number of sections to something greater than one, I get the number of sections in grouped style but all the cells repeat in each section. How do I setup the table so each cell is in a section?

Also, is it possible to programmatically set the table style to grouped?

+2  A: 

For your second question:

tableView.style = UITableViewStyleGrouped;

Or, if you're creating it programmatically:

tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];

For your first question, I assume you are setting up your cells something like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    ...
    setupCell(cell,indexPath.row);
    return cell;
}

Where setupCell is something that checks the index and sets the cell accordingly. The IndexPath, however, tells you which row in which section to set up.

I also assume that you're returning the full number of rows in the function

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

Where you should only be returning 1, since you want exactly 1 row in each section.

Then when setting up your cell, you'd want to check the indexPath.section instead of the indexPath.row. Rows are zero-based by section, so the calls to cellForRowAtIndexPath will have index paths that look like this:

Section Row
0, 0
1, 0
2, 0

Whereas originally, when you had only one section, it would have been:
0, 0
0, 1
0, 2

And what you're seeing right now, since you return the same number of rows as sections is:
0, 0
0, 1
0, 2

1, 0
1, 1
1, 2

2, 0
2, 1
2, 2

Ed Marty
A: 

actually, you can't do this:

tableView.style = UITableViewStyleGrouped;

because style is a readonly property.

God of Biscuits