【问题标题】:How to implement didSelectRowAtIndexPath method with sectioned UITableView如何使用分段 UITableView 实现 didSelectRowAtIndexPath 方法
【发布时间】:2013-09-12 00:31:03
【问题描述】:

我一直在实现一个带有分段 UITableView 的应用程序。在 tableview 中,我使用了一个名为 section 的数组来存储不同的 classes 数组并在不同的 section 中显示它们。另一方面,我还使用了一个名为 headers 的数组来存储节标题的名称。比如“weekday”部分,这个部分下的tableViewCell中有7个词,Sunday到Saturday。在 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];
}

在这个 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; 
}

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

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

【问题讨论】:

    标签: objective-c uitableview didselectrowatindexpath


    【解决方案1】:

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

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

    注意:使用AppDelegate 在视图控制器之间共享数据不是一个好主意。考虑将数据分离到单独的模型类and make that class a singleton

    【讨论】:

    • 能否请您指导我如何使用 ContentViewController 中 viewDidLoad 中的 index.section 方法以及如何确定 delegate.vcblr 到 section 的索引?我尝试修改我的代码,如下所示:NSDictionary *voc_list=[[delegate.vcblr objectAtIndex:index.section]objectAtindex:index.row]; 该应用程序将崩溃。
    • @Tek 你的vcblr 组织得如何?假设您有两个部分,其中包含 section 0:(a,b,c)section 1:(x,y) 项目,vcblr 是否会包含 (a,b,c,x,y)
    • 感谢您的及时帮助! vcblr 在 AppDelegate.h 中被声明为 NSMutableArray。这个数组也是从 JSON 数组传输过来的:AppDelegate 中的NSArray *vocabulary_ = [json objectForKey:@"vocabulary"];self.vcblr=[NSArray arrayWithArray:vocabulary_];。 “部分”日志部分如下:((周一、周二、周三、周四、周五、周六、周日),(自行车、公共汽车、汽车、火车、出租车))
    • @Tek 那么你的vcblr 确实是一个数组数组,所以你的代码[[delegate.vcblr objectAtIndex:index.section]objectAtindex:index.row] 应该可以工作。如果它的组织方式与您的主控制器中的 self.sections 数组一样,那么双索引应该没问题。在调试器中运行,设置断点,然后在 Xcode 中查看数组的结构。
    • 控制台窗口将显示以下信息:无法识别的选择器发送到实例 0x7a73e80'。我查看了本站的一些类似帖子,想知道是否需要在 didSelectRowAtIndexPath 方法中添加if (indexPath.section == __) 条件语句?
    猜你喜欢
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 2012-01-14
    • 2012-07-29
    相关资源
    最近更新 更多