自定义数据驱动的TableView

发布于 2024-08-11 16:32:43 字数 620 浏览 2 评论 0原文

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

示例:

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

使用设置菜单作为指南。有一些选项在一个部分中是单行,因此它们看起来像是一个按钮...

如果这没有意义,我会尽力解释得更好。


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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深陷 2024-08-18 16:32:43
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    如果(部分== 0){
        返回1;
    }别的{
        return [国家/地区数据计数];
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}   

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

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

空‖城人不在 2024-08-18 16:32:43

您只需要更新 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;
}

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;
}
捶死心动 2024-08-18 16:32:43

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文