代码之家  ›  专栏  ›  技术社区  ›  rson

自定义数据驱动的TableView

  •  0
  • rson  · 技术社区  · 16 年前

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

    例子:

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


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

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

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

    3 回复  |  直到 16 年前
        1
  •  1
  •   Morion    16 年前
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if(section == 0){
            return 1;
        }else{
            return [countrysData count];
        }
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 2;
    }   
    

        2
  •  1
  •   gerry3    16 年前

    例如,下面是如何更新 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;
    }
    
        3
  •  0
  •   TechZen    16 年前

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