Search code examples
iphoneuitableviewtableviewcocoa-design-patterns

Customize a data-driven TableView


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.


Solution

  • - (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.