Objective c 选择行时,TableView返回null(0)

Objective c 选择行时,TableView返回null(0),objective-c,ipad,uitableview,Objective C,Ipad,Uitableview,我有一个表视图控件,我已经对它进行了分区/分组。但是,这已经破坏了决定我在表中选择的项目的代码 - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { /* When a row is selected, set the detail view controller's detail item to the item associated wi

我有一个表视图控件,我已经对它进行了分区/分组。但是,这已经破坏了决定我在表中选择的项目的代码

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    /*
     When a row is selected, set the detail view controller's detail item to the item associated with the selected row.
     */
    //detailViewController.detailItem = [NSString stringWithFormat:@"Row %d", indexPath.row];
    if (_delegate != nil) {
        Condition *condition = (Condition *) [_conditions objectAtIndex:indexPath.row];
        [_delegate conditionSelectionChanged:condition];
    }
}
我的示例数据目前有6个部分,第一个部分包含两个项,其余部分包含一个项。如果我在第一部分中选择第二个项目,则详细信息视图会很好地更新,任何其他选择都会在第一部分中显示第一个项目(项目0)

我假设这是因为indexPath.row为其他部分返回0,因为它是该部分中的第一项,这将导致选择数组中的第一个对象

如何修复代码以确保选择了正确的项目?除此之外,其他一切都很好

cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CellIdentifier";

    // Dequeue or create a cell of the appropriate type.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    //get the letter in the current section
    NSString *alphabet = [conditionIndex objectAtIndex:[indexPath section]];

    //get all conditions beginning with the letter
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name beginswith[c] %@", alphabet];
    NSArray *conditionsGrouped = [_conditions filteredArrayUsingPredicate:predicate];

    if ([conditionsGrouped count] > 0) {
        Condition *condition = [conditionsGrouped objectAtIndex:indexPath.row];
        cell.textLabel.text = condition.name;
    }

    return cell;
}

您还应该考虑indexPath.section


您应该创建一个二维条件数组,先按节索引,然后按行索引。

按照本文的答案进行操作:

谢谢你的帮助。你能给我一些想法,让我开始或指向一个教程或样本,我正在学习这一点,因为我一直在进行?没有教程为您的特殊需要。您是如何实现CellForRowatineXpath的?我已经在上面的问题中添加了该代码。谢谢这似乎也会导致搜索问题,因为只返回第一节中的项目。这就是问题所在:您使用第一个字母逐节显示行,而您在选择时没有执行相同的操作。