【问题标题】:NSMutableArray and NSMutableDictionary issueNSMutableArray 和 NSMutableDictionary 问题
【发布时间】:2013-01-22 07:40:17
【问题描述】:

我正在实现分段 tableview 的概念并在特定点上受到打击。 我有一个 NSMutableArray 有 2 个这样的对象:

<__NSArrayI 0xabf8c00>(
    {
        City = "SanFransisco ";
        Latitude = "28.602704";
        Longitude = "-81.430760";
        State = California;
    },
    {
        City = Orlando;
        Latitude = "28.49379";
        Longitude = "-81.474251";
        State = Florida;
    },

    )
    ,
    <__NSCFArray 0xab38ec0>(
    {
        geometry =     {
            location =         {
                lat = "37.789632";
                lng = "-122.417004";
            };
        };
        name = "St. Francis Memorial Hospital";
        vicinity = "900 Hyde Street, San Francisco";
    },
    {
        geometry =     {
            location =         {
                lat = "37.794363";
                lng = "-122.395784";
            };
        };
        name = "St Memorial Hospital";
        vicinity = "5 Embarcadero Center, San Francisco";
    },
    {
        geometry =     {
            location =         {
                lat = "37.788297";
                lng = "-122.409653";
            };
        };
        name = "Global Memorial Hospital";
        vicinity = "490 Post Street #520, San Francisco";
    },
    )

我在 tableView 的两个部分中加载这 2 个对象。但是,我不知道如何比较和获取第一个对象的每个键(城市、纬度、经度)和第二个对象的纬度、lng、附近的 NSMutableDictionary。我已经这样做了,但出现错误:**SIGABRT error**

编辑:

- (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]autorelease];
        cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;



    }

    NSMutableArray *array = [listOfItems objectAtIndex:indexPath.section];
    int section=indexPath.section;


    if(section==0)
    {

        for (int i=0;i<[array count];i++)
        {

        cell.textLabel.text = [[array  objectAtIndex:i] objectForKey:@"City"] ;

        cell.textLabel.tag=3;
        cell.textLabel.font=[UIFont boldSystemFontOfSize:12];

        NSString *lat=[NSString stringWithFormat:@"%@",[[array  objectAtIndex:i] objectForKey:@"Latitude"]];
        UILabel *latitude = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, 320, 10)];
        latitude .font=[UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:12];
        [latitude  setTextAlignment:UITextAlignmentLeft];
        latitude.text=[NSString stringWithFormat:@"%@",lat];
        latitude.tag=1;
        [cell addSubview:latitude ];
        latitude.hidden=YES;
        [latitude release];


        NSString *lng=[NSString stringWithFormat:@"%@",[[array  objectAtIndex:i] objectForKey:@"Longitude"]];
        UILabel *longitude = [[UILabel alloc]initWithFrame:CGRectMake(10, 80, 320, 10)];
        longitude .font=[UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:12];
        [longitude  setTextAlignment:UITextAlignmentLeft];
        longitude.text=[NSString stringWithFormat:@"%@",lng];
        longitude.tag=2;
        [cell addSubview:longitude ];
        longitude.hidden=YES;
        [longitude release];


         return cell;

    }
    }

    if(section==1)
    {




        for (int i=0;i<[array count];i++)
        {


             cell.textLabel.text =[[array  objectAtIndex:i] objectForKey:@"name"] ;

            cell.textLabel.lineBreakMode=UILineBreakModeWordWrap;
            cell.textLabel.numberOfLines=0;
            cell.textLabel.font=[UIFont boldSystemFontOfSize:12];
            cell.textLabel.tag=3;



            NSMutableDictionary * location = [[[array objectAtIndex:indexPath.row]objectForKey:@"geometry"]objectForKey:@"location"];
            NSString * lat = [location objectForKey:@"lat"];
            NSString * lng = [location objectForKey:@"lng"];

            UILabel *latitude = [[UILabel alloc]initWithFrame:CGRectMake(10, 80, 320, 10)];
            latitude .font=[UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:12];
            [latitude  setTextAlignment:UITextAlignmentLeft];
            latitude.text=[NSString stringWithFormat:@"%@",lat];

            latitude.tag=1;
            [cell addSubview:latitude ];
            latitude.hidden=YES;
            [latitude release];



            UILabel *longitude = [[UILabel alloc]initWithFrame:CGRectMake(10, 85, 320, 10)];
            longitude .font=[UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:12];
            [longitude  setTextAlignment:UITextAlignmentLeft];
            [longitude  setText:[NSString stringWithFormat:@"%@",lng]];
            longitude.tag=2;
            [cell addSubview:longitude ];
            longitude.hidden=YES;
            [longitude release];


            NSString *vicinity1 = [[array objectAtIndex:indexPath.row]objectForKey:@"vicinity"];
            UILabel *vicinity = [[UILabel alloc]initWithFrame:CGRectMake(10, 45, 320, 10)];
            vicinity .font=[UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:12];
            [vicinity  setTextAlignment:UITextAlignmentLeft];
            vicinity.text=[NSString stringWithFormat:@"%@",vicinity1];

            vicinity.tag=4;
            [cell addSubview:vicinity ];
            vicinity.hidden=YES;
            [vicinity release];



             return cell;



        }


    }



}

这次我哪里出错了?

【问题讨论】:

  • 您从哪一行代码中得到了 SIGABRT 错误?
  • 在 section0 这一行 cell.textLabel.text = [dictionary objectForKey:@"City"] .
  • 也许它一文不值,但是从你的转储中你没有一个 MUTABLE 数组:根对象类是 __NSArrayI 它是 IMMUTABLE 对象的内部类(又名 NSArray)
  • K 那我该怎么办?

标签: iphone uitableview xcode4.5


【解决方案1】:

由于您的错误,我了解到您的变量字典实际上不是字典而是数组。

尝试使用这个:

NSMutableArray *array = [listOfItems objectAtIndex:indexPath.row];
cell.textLabel.text = [[array  objectAtIndex:0] objectForKey:@"City"];

如果这会返回某些东西(或某个名称),也许您可​​以相应地更改您的逻辑并获得所需的结果。

让我知道它是否有效:)

编辑:

int section=indexPath.section;


    if(section==0)
    {
        NSMutableArray *array = [listOfItems objectAtIndex:indexPath.row];

        for (int i=0;i<[array count];i++)
        {

        cell.textLabel.text = [[array  objectAtIndex:i] objectForKey:@"City"] ;

        cell.textLabel.tag=3;
        ....

indexPath.section 将始终返回 0

试试这个:)

【讨论】:

  • 是的..我也认为这就是问题所在。另外,请检查您的numberOfSectionsInTableViewnumberOfRowsInSection 是否返回正确的值..
  • 我只是在检查它。一旦 cmpltd 我会通知你
  • Hey vshall prevoius 问题已解决,但现在是另一个问题。实际上,我在两个部分的所有单元格中都重复获取数据。我已经编辑了我的问题..你能看看
  • 你可以检查你所在的部分,然后根据部分,你可以填充你的数组。
  • 是的,一旦我也解决了这个问题,我肯定会投赞成票。我已经在 cellforrowatindexpath 部分编辑了我的问题。你能检查一下并说出我哪里出错了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-01
  • 2013-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多