0

我是 iPhone 开发的新手。

我有数组字典。

{
    A =     (
               {
                address = "Talala (gir)";
                "main_id" = 1;
                mobile = 8878876884;
                name = "Amit Patel";
               },
               {
                address = "Junagdh";
                "main_id" = 5;
                mobile = 4894679865;
                name = "Arjun Patel";
               }
            );
        J = (
                {
                address = "Taveli";
                "main_id" = 6;
                mobile = 87886356085878;
                name = "Jasmin Patel";
                },
                {
                address = "Gujarat";
                "main_id" = 4;
                mobile = 6636633368;
                name = "Jatin ";
               }
           );
        R =     (
                    {
                address = "Mumbai";
                "main_id" = 2;
                mobile = 999686322;
                name = "Rajan Patel";
            }
        );
        S =     (
                    {
                address = "Rajkot";
                "main_id" = 3;
                mobile = 8866086549;
                name = "Sumit Patel";
            }
        );
    }

我有UITableView并且我想显示有键"name"的值UITableViewCell

我的cellForRowAtIndexPath方法是

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [[[self.finalPDic objectForKey:[self.listOfHeader objectAtIndex:indexPath.row]] objectAtIndex:indexPath.row] objectForKey:@"name"];
    return cell;
}

但是上面的代码对我来说是粉碎的。

4

2 回答 2

1

这段代码有很多问题......

第一个 indexPath.row 必须是indexPath.section

作为一般提示,最好组织您的代码更具可读性,例如:

NSArray* nameArrayForLetter = [self.finalPDic objectForKey:[self.listOfHeader objectAtIndex:indexPath.section];
NSDictionary* addressDict = [nameArrayForLetter objectAtIndex:indexPath.row];
NSString* nameString = [addressDict objectForKey:@"name"];
于 2013-04-17T07:06:07.433 回答
0

您的代码一一遍历根键,但您不会从较低级别的索引 0 开始。(S 键是第 4 个,所以你想要第 3 项它不存在的数组。

尝试使用部分。A..S 是部分,行是​​名称!

于 2013-04-17T07:03:44.847 回答