iOS - 查找节中单元格的索引

时间:2014-01-03 23:14:51

标签: ios row tableview

我有一个表格除以动态更改按字母顺序排列的部分。当我按下一个单元格时,我希望它能够展开。

发生的事情是,例如,当我在第2部分中选择cell1时,每个部分中的每个cell1都会打开。

我知道我需要在didselectrowforindexpath中获取indexpath.section和indexpath.row的值,但是如何?

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString* letter = [letters objectAtIndex:indexPath.section];
NSArray* arrayForLetter = (NSArray*)[filteredTableData objectForKey:letter];
shu = (ShuDict*)[arrayForLetter objectAtIndex:indexPath.row];

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

    if (!self.detailViewController) {
        self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController_iPhone" bundle:nil];
    }
    [self.navigationController pushViewController:self.detailViewController animated:YES];
} else {

    NSString* letter = [letters objectAtIndex:indexPath.section];
    NSArray* arrayForLetter = (NSArray*)[filteredTableData objectForKey:letter];
    shu = (ShuDict*)[arrayForLetter objectAtIndex:indexPath.row];
    NSLog(@"%@",shu);
    self.detailViewController.detailItem = shu.description;
    self.detailViewController.title = shu.title;

}

    if (selectedRow == indexPath.row) {
        lblDecs.hidden=NO;
        selectedRow = -1;

    }
    else{
        lblDecs.hidden = YES;
        selectedRow = indexPath.row;

    }

    [tableView reloadData];
}

所以这是我的didSelectRowAtIndexPath方法。我认为问题是selectRow == indexpath.row,因为它不包含该部分的索引?

1 个答案:

答案 0 :(得分:0)

我认为“lblDecs”很常见。它不会改变任何细胞的选择

// Original code
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
       .
       .
       .  
       .
   if (selectedRow == indexPath.row) {
        lblDecs.hidden=NO;
        selectedRow = -1;

    }
    else{
        lblDecs.hidden = YES;
        selectedRow = indexPath.row;

    }
 }

所以你可以直接显示lblDecs

lblDecs.hidden=NO;

并隐藏方法

 - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    lblDecs.hidden=YES;
}

如果lblDecs与使用此

不同
if ((selectedRow == indexPath.row) && (selectedSection == indexPath.section) ) {
        .
        .
        .
 }