【问题标题】:didSelectRowAtIndex issuedidSelectRowAt 索引问题
【发布时间】:2011-05-09 05:39:48
【问题描述】:

我有一个UITableView,我在其中添加了部分。当使用didSelectRowAtIndexindexPath.row 时,我没有得到正确的值。假设我在第 2 部分,然后它再次从 0 开始行索引 (indexPath.row),因此我得到了错误的索引。

假设第一个数组有 4 个名称:(0)50 street、(1)apple、(2)boy 和 (3)cat。当您选择任何人时,它会根据索引 (0,1,2,3) 加载正确的详细视图。但是,当您去搜索并键入“b”时,它会将表格缩小到现在位于第 0 行的 (2)boy。因此,当您选择 50 street 时,它会从原始数组中加载 boy。

谁能告诉我如何解决这个问题? 我意识到可以通过indexPath.section 获取部分索引,但我不知道如何使用它。

【问题讨论】:

  • 阅读一些有关表格视图的文章并集中注意力。请参阅此链接以获取表格视图教程。

标签: iphone xcode didselectrowatindexpath


【解决方案1】:

您可以通过将上述部分的计数相加来计算“从顶部开始的总行数”,即。 int j=0;for (int i=0;i<indexPath.section;i++) {j=j+[tableView numberOfRowsInSection:i];j=j+indexPath.Row;}

【讨论】:

    【解决方案2】:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) // First section
    {
    //Do something
    }
    else if(indexPath.section == 1) // Second Section
    {
    //Do something 
    }
    }
    

    【讨论】:

    • 目录 *aDirectory = [appDelegate.directories objectAtIndex:indexPath.section];问题是我将我的 xml 数据存储在 appdelegate 中,并创建了一个目录来存储我的所有 appDelegate(xml 数据),然后我显示在 _XMLDetailsView.aDirectory = aDirectory;其中 xmldetailsview 目录值将与 appDelegate 中的值相同。但是数组从 0 开始,而不是从第 1 节(第 2 节)开始的数组(1)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    • 2017-01-01
    • 2011-06-18
    • 2013-07-03
    相关资源
    最近更新 更多