English 中文(简体)
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.

最佳回答
- (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.

问题回答

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;
}

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.





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签