ifraaank
ifraaank

Reputation: 122

Adding sections in a grouped table view in Xcode

My code is a little different to others, but it works.

I am new to app coding, but I would like to add some of these into sections:

enter image description here

So some of them have their own group with a small title to each.

But my code looks like this:

enter image description here

and I don't know what to insert to do it right.

(The bottom half of that picture is the pictures in the detail view, that shows up in the detail view when you select something from the table view.)

(I know Xcode shows errors in my code, but it still works. )

Can anyone help me?

Upvotes: 0

Views: 9570

Answers (1)

Oliver
Oliver

Reputation: 23550

You have to implement some UITableView methods and delegate methods (and of course set your class as the delegate of the class) :

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //Here you must return the number of sectiosn you want
 }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //Here, for each section, you must return the number of rows it will contain
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
     //For each section, you must return here it's label
     if(section == 0) return @"header 1"; 
     .....
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell...
    cell.text = // some to display in the cell represented by indexPath.section and indexPath.row;

    return cell;
}

With that, you can arrange your data as you want : One array for each section, one big array with sub arrays, ... as you want. Antthing will be ok as far as you can return the wanted values from the methods above.

Upvotes: 1

Related Questions