自定义数据驱动的TableView

问题描述:

我有一个分组的tableview,在一个部分填充了XML数据。我想要做的是在数据驱动之前创建另一个部分,然后对其应用一个操作。自定义数据驱动的TableView

例子:

用户呈现一个按钮,说“用你的当前位置”(手动创建部分)再下面是国家的列表,用户也可以选择从(数据驱动部分选择)

使用设置菜单作为指导。有一些选项,这些选项是在部分单行,所以他们很显然是一个按钮...

如果这没有任何意义,我会尽量解释更好。


所以我的代码,这两个明显的线......很简单

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

我想什么是有numberOfSectionsInTableView回报2,并有第一个“科”说“点击使用你当前的位置“,然后将其推入地图中,第二部分显示我目前正在工作的国家/地区列表。

+0

是的,这将是很高兴看到一些细节。 – Morion 2009-11-17 15:36:35

+0

那么你的问题究竟是什么? – 2009-11-17 15:41:55

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

,那么你应该选择什么在您的didSelectRowAtIndexPath方法做:法因indexPath.section。哦,你应该在你的cellForRowAtIndexPath:方法中检查indexPath.section。

+0

我希望我可以选择你和gerry3的答案,因为我使用了两者的组合。我选择你的是因为你的numberOfRowsInSection方法没有给我任何警告,而且gerry的确做到了: -/ – rson 2009-11-17 17:25:45

+0

警告并不重要,但现在应该修复。我至少能得到一个投票吗? :-) – gerry3 2009-11-19 10:44:48

你只需要更新所有的UITableViewDataSource和的UITableViewDelegate协议,适当考虑新的部分,它的行(或多个)实现的。

例如,这里是如何更新numberOfSectionsInTableView:,​​3210,并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; 
} 

我敢肯定,你可以改变UITableViewDataSource的方法的返回'numberOfSectionsInTableView:'在飞行中。一旦用户选择了附加部分的选择,只需设置一个标志让该方法返回您想要的表的数量。然后你强制重新加载表格,你应该看到新的部分。