views:

367

answers:

3

I have a grouped tableview that is populated with XML data in one section. What I would like to do is create another section prior to the data driven one, and apply an action to it.

Example:

The user is presented with a button that says "use your current location" (manually created section) and below that is a list of countries the user can alternatively choose from choose from (data driven section)

Use the settings menu as a guide. There are some options which are a single row in a section, so they appear to be a button...

If this doesn't make sense, I will try to explain it better.


So I have these two obvious lines of code...simple enough

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}    
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [countrysData count];
    }

What I would like is to have numberOfSectionsInTableView return 2 and have the first "Section" say "Click to use your current location" which would then push into view a map, and the second section display the list of countries I currently have working.

A: 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0){
        return 1;
    }else{
        return [countrysData count];
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

then you should choose what to do in your didSelectRowAtIndexPath: method due to the indexPath.section. oh, and you should check indexPath.section in your cellForRowAtIndexPath: method.

Morion
I wish I could choose both yours and gerry3's answers, because I used a combination of both. I chose yours because your numberOfRowsInSection method didn't give me any warnings, and gerry's did :-/
rson
Warning didn't matter, but should be fixed now. Can I at least get an up vote? :-)
gerry3
A: 

You just need to update all of your implementations of the UITableViewDataSource and UITableViewDelegate protocols to appropriately account for the new section and its row(s).

For example, here's how to update numberOfSectionsInTableView:, tableView:numberOfRowsInSection:, and tableView:cellForRowAtIndexPath:, but you will want to update at least tableView:didSelectRowAtIndexPath: as well:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // calculate the number of sections of non-data (might just be 1)    
    // calculate the number of sections for the data (you were already doing this, might just be 1)
    // return the sum
    return 1 + 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger rowCount = 0;
    switch (section) {
    case 0:
        // non-data section has 1 row/cell
        rowCount = 1;
        break;
    case 1:
        // data section uses an array
        rowCount = [dataArray count];
        break;
    }
    return rowCount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *nonDataCellID = @"NonDataCell";
    static NSString *dataCellID = @"DataCell";
    UITableViewCell *cell;

    int section = [indexPath indexAtPosition:0];
    int row = [indexPath indexAtPosition:1];

    switch (section) {
        case 0:
            // or you can just use standard cells here
            cell = [tableView dequeueReusableCellWithIdentifier:nonDataCellID];
            if (cell == nil) {
                [[NSBundle mainBundle] loadNibNamed:@"NonDataCell" owner:self options:NULL];
                cell = nonDataCell; // nonDataCell is an IBOutlet to this custom cell
            }

            // configure non-data cell here (use tags)
            UILabel *someLabel = (UILabel*)[cell viewWithTag:1];
            someLabel.text = @"Non-data cell";
            break;

        case 1:
            // or you can just use standard cells here
            cell = [tableView dequeueReusableCellWithIdentifier:dataCellID];
            if (cell == nil) {
                [[NSBundle mainBundle] loadNibNamed:@"dataCell" owner:self options:NULL];
                cell = dataCell; // dataCell is an IBOutlet to this custom cell
            }

            // configure data call here (using "row")
            UILabel *someDataLabel = (UILabel*)[cell viewWithTag:1];
            someDataLabel.text = [[dataArray objectAtIndex:row] valueForKey:@"name"];
            break;
       }

    return cell;
}
gerry3
A: 

I'm pretty sure you can alter the return of UITableViewDataSource's method 'numberOfSectionsInTableView:' on the fly. Once the user selects the choice of an additional section, just set a flag to have the method return the number of tables you want. Then you force a reload of the table and you should see the new section.

TechZen