【问题标题】:indexpath of cells in grouped tableview分组表视图中单元格的索引路径
【发布时间】:2010-12-14 12:01:17
【问题描述】:

我有一个表格视图,按字母顺序划分,从已过滤的数组中填充

如何跟踪分组表视图中单元格的“真实”索引路径。 而不是每个部分从 0 开始计数。

我需要得到这个,这样我才能从数组中访问正确的数据位

目前它是这样工作的(以防我不清楚)

(第 0 节) 第 0 行 第 1 行 第 2 行 (第 1 节) 第 0 行 第 1 行 等等…………

我需要得到它,这样我才能得到

第 0 节) 第 0 行 第 1 行 第 2 行 (第 1 节) 第 3 行 第 4 行

提前致谢

【问题讨论】:

    标签: iphone objective-c uitableview ios4


    【解决方案1】:

    试试这个:

    - (NSUInteger)indexFromIndexPath:(NSIndexPath*)indexPath
    {
        NSUInteger index=0;
        for( int i=0; i<indexPath.section; i++ )
            index += [self tableView:self.tableView numberOfRowsInSection:i];
    
        index += indexPath.row;
    
        return index;
    }
    

    假设这个方法被放置在一个 UITableViewController 类中。 你可以这样使用它:

    NSUInterger realIndex = [self indexFromIndexPath:indexPath];
    id myObject = [dataArray objectAtIndex:realIndex];
    

    【讨论】:

      【解决方案2】:

      使用此代码获取正确的行,

      NSUInteger row = 0;
      for (NSUInteger i = 0; i < noofsectionsinTableView; i++)
         row += [self.tableView numberOfRowsInSection:i];
      return row;
      

      【讨论】:

      • 对不起,你说的数据源是什么意思? ,表格视图,包含我的数据的数组,既不响应 numberOfRowsInSection ---对不起,如果我很愚蠢
      • 很抱歉,你有一个例子说明你是如何使用它的吗?
      • 我可以知道你想在哪里实现这个吗?
      • 非常感谢您的帮助,但我使用了 jens 答案,但您的帮助很大
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 2011-02-02
      • 2012-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多