Objective c 如何用分段UITableView实现didSelectRowAtIndexPath方法

Objective c 如何用分段UITableView实现didSelectRowAtIndexPath方法,objective-c,uitableview,didselectrowatindexpath,Objective C,Uitableview,Didselectrowatindexpath,我一直在用分区UITableView实现一个应用程序。在tableview中,我使用了一个名为sections的数组来存储不同的类数组,并在不同的节中显示它们。另一方面,我还使用了一个名为headers的数组来存储节头的名称。例如,在“工作日”部分中,该部分下的tableViewCell中有七个单词,从星期日到星期六。在MainViewController中: -(void)viewDidLoad { [super viewDidLoad]; NSMutableArray *w

我一直在用分区UITableView实现一个应用程序。在tableview中,我使用了一个名为sections的数组来存储不同的类数组,并在不同的节中显示它们。另一方面,我还使用了一个名为headers的数组来存储节头的名称。例如,在“工作日”部分中,该部分下的tableViewCell中有七个单词,从星期日到星期六。在MainViewController中:

-(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];
}
-(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.....
}
在这个应用程序中,我还实现了导航控制器,它可以让用户通过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; 
}
在ContentViewController中:

-(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];
}
-(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_列表开始加载信息。也就是说,tableViewCell不响应正确的详细信息内容。有人能给我一些解决问题的办法吗

我想知道为什么每个部分都会从voc_列表开始加载信息

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


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

你能告诉我如何在CopeDeVelueCopyValueCube中使用Velux.Fielt方法,以及如何决定Cuto?VCBLR到节的索引?我尝试修改代码,如下所示:NSDictionary*voc_list=[[delegate.vcblr objectAtIndex:index.section]objectAtIndex:index.row];应用程序将崩溃。@Tek您的vcblr是如何组织的?假设您有两个部分,分别为第0部分:a、b、c和第1部分:x、y,vcblr是否包含a、b、c、x、y?感谢您的及时帮助!vcblr在AppDelegate.h中声明为NSMutableArray。此数组也是从JSON数组传输的:NSArray*词汇表\=[JSON objectForKey:@词汇表];self.vcblr=[NSArray arrayWithArray:vocagex_];在AppDelegate中。“部分”日志的部分如下:星期一、星期二、星期三、星期四、星期五、星期六、星期天,自行车、公共汽车、汽车、火车、出租车@Tek那么你的vcblr实际上是一个数组,所以你的代码[[delegate.vcblr objectAtIndex:index.section]objectAtIndex:index.row]应该可以工作。如果它的组织方式与主控制器中的self.sections数组完全相同,则双索引应该可以。在调试器中运行,设置断点,并在Xcode中查看数组的结构。控制台窗口将显示以下信息:发送到实例0x7a73e80的无法识别的选择器。我已检查了一些类似的pOST在此站点中,并想知道是否有必要在didSelectRowAtIndexPath方法中添加if indexath.section==\uuuu条件语句?