سؤال

I have been implementing a app with sectioned UITableView. In the tableview, I used a array, called sections, to store different classes array and show them in different sections. On the other hand, I also used a array, called headers, to store section headers' name. For example, in the "weekday" section, there are seven words, Sunday to Saturday, in the tableViewCell under this section. In the 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];
}

In this App, I also implemented the navigation controller, which can bring user to view detail information in the didSelectRowAtIndexPath method. The codes are following

-(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; 
}

In the ContentViewController:

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

I am wondering why each section will load the information from beginning in the voc_list. That is to say, the tableViewCell do not respond to the right detail content. Could anyone provide me with ideas to resolve it?

هل كانت مفيدة؟

المحلول

I am wondering why each section will load the information from beginning in the voc_list.

Because the code of your viewDidLoad method ignores index.section, paying attention to index.row alone. You need to decide on the index that you pass to delegate.vcblr based on both the section and the row; the exact calculation depends on the structure of your data, i.e. how many rows you have in each section.

Note : Using AppDelegate to share data among view controllers is not a good idea. Consider separating out your data in a separate model class, and make that class a singleton.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top