0

我有一个分组的表格视图,它在一个部分中填充了 XML 数据。我想做的是在数据驱动的部分之前创建另一个部分,并对其应用操作。

例子:

用户会看到一个按钮,上面写着“使用您当前的位置”(手动创建的部分),下面是用户可以选择的国家列表(数据驱动部分)

使用设置菜单作为指南。有一些选项是一个部分中的单行,所以它们似乎是一个按钮......

如果这没有意义,我会尝试更好地解释它。


所以我有这两行明显的代码......足够简单

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

我想要numberOfSectionsInTableView返回 2 并让第一个“部分”说“单击以使用您当前的位置”,然后将其推入查看地图,第二部分显示我目前正在工作的国家/地区列表。

4

3 回答 3

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

那么由于 indexPath.section,您应该在 didSelectRowAtIndexPath: 方法中选择要执行的操作。哦,你应该在你的 cellForRowAtIndexPath: 方法中检查 indexPath.section。

于 2009-11-17T16:11:11.263 回答
1

您只需要更新 UITableViewDataSource 和 UITableViewDelegate 协议的所有实现,以适当地考虑新部分及其行。

例如,以下是如何更新numberOfSectionsInTableView:tableView:numberOfRowsInSection:tableView:cellForRowAtIndexPath:,但您至少需要更新tableView:didSelectRowAtIndexPath:

- (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;
}
于 2009-11-17T16:11:45.520 回答
0

我很确定您可以即时更改 UITableViewDataSource 方法“numberOfSectionsInTableView:”的返回。一旦用户选择了附加部分的选项,只需设置一个标志以使该方法返回您想要的表数。然后你强制重新加载表格,你应该会看到新的部分。

于 2009-11-17T16:12:52.470 回答