1

我一直在用分段的 UITableView 实现一个应用程序。在 tableview 中,我使用了一个名为 section 的数组来存储不同的 classes 数组并在不同的 section 中显示它们。另一方面,我还使用了一个名为 headers 的数组来存储节标题的名称。比如“weekday”部分,这个部分下的tableViewCell中有7个词,Sunday到Saturday。在主视图控制器中:

-(void)viewDidLoad
{
    [super viewDidLoad];

    NSMutableArray *weekday=[[NSMutableArray alloc]init];
    NSMutableArray *traffic=[[NSMutableArray alloc]init];

    for (NSArray *sec_ary in listData) {
    NSString *class=[[sec_ary valueForKey:@"vocabulary_list"]valueForKey:@"Class"];
    if ([class isEqualToString:@"WEEKDAY"]) {
       [weekday addObject:[[sec_ary valueForKey:@"vocabulary_list"]valueForKey:@"Vocabulary"]];
    }else if ([class isEqualToString:@"TRAFFIC TOOLS"]){
        [traffic addObject:[[sec_ary valueForKey:@"vocabulary_list"]valueForKey:@"Vocabulary"]];
    }
    sections=[[NSArray alloc ]initWithObjects:weekday,traffic,nil];
    headers=[[NSArray alloc]initWithObjects:@"WEEKDAY",@"TRAFFIC TOOLS",nil];
}

在这个 App 中,我还实现了导航控制器,它可以让用户在 didSelectRowAtIndexPath 方法中查看详细信息。代码如下

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
      AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
      ContentViewController *content= [[ContentViewController alloc] initwithIndexPath:indexPath];
      [delegate.navController1 pushViewController:content animated:YES];
      [tableView deselectRowAtIndexPath:indexPath animated:YES];  
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
      static NSString *TableIdentifier = @"tableidentifier"; / 
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableIdentifier]; 

      if (cell == nil) { 
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle          reuseIdentifier:TableIdentifier] autorelease];
      } 
      cell.textLabel.text = (NSString*)[[self.sections objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
      cell.textLabel.font = [UIFont boldSystemFontOfSize:15];

      return cell; 
}

在内容视图控制器中:

-(void)viewDidLoad{
      [super viewDidLoad];
      AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
      NSDictionary *voc_list=[delegate.vcblr objectAtIndex:index.row];
      //extracting the voc_list dictionary to show infomation.....
}

我想知道为什么每个部分都会从 voc_list 的开头加载信息。也就是说,tableViewCell 没有响应正确的详细内容。任何人都可以为我提供解决它的想法吗?

4

1 回答 1

0

我想知道为什么每个部分都会从voc_list.

因为你viewDidLoad方法的代码忽略,单独index.section注意。您需要根据和index.row来决定传递给的索引;确切的计算取决于您的数据结构,即每个部分中有多少行。delegate.vcblrsectionrow

注意:使用AppDelegate在视图控制器之间共享数据不是一个好主意。考虑将数据分离到单独的模型类中,并使该类成为 singleton

于 2013-09-12T00:50:20.590 回答