首先从几行中获取tableview中的正确值

时间:2013-04-18 07:48:16

标签: iphone ios uitableview nsarray nsdictionary

NSMutableArray *wholeJsonArray = [LoginResult objectForKey:@"Response"];
//NSMutableArray *values=[NSMutableArray arrayWithArray:[LoginResult allValues]];    
NSMutableArray *statenameArray=[LoginResult objectForKey:@"Response"];
for(NSDictionary *countname in wholeJsonArray)
{

    NSString *cName = [NSString stringWithFormat:@"%@",[countname objectForKey:@"country_name"]];
    [countryArray addObject:cName];

}
for(NSDictionary *cid in wholeJsonArray)
{

    NSNumber *number = [cid objectForKey:@"country_id"];
    [idcountry addObject:number];
}

for(NSDictionary *statename in statenameArray)
{

    NSString *sName=[NSString stringWithFormat:@"%@",[statename objectForKey:@"state_name"]];
    [stateArray addObject:sName];
}

我写了上面的代码。

国家/地区名称在表格视图中正确显示。状态名称响应也会显示,但显示的行数为空值,其他行具有适当的值。那么我该怎么做才能从几行中删除空值。

提前致谢。

3 个答案:

答案 0 :(得分:0)

将最后for替换为:

for (NSDictionary *statename in statenameArray) {
    NSString * sName = [statename objectForKey:@"state_name"];
    if (sName.length > 0) {
        [stateArray addObject:[NSString stringWithFormat:@"%@", sName]];
    }
}

答案 1 :(得分:0)

仅当状态字符串有效时才将其添加到数组中。

for(NSDictionary *statename in statenameArray)
{
   NSString *sName=[NSString stringWithFormat:@"%@",[statename objectForKey:@"state_name"]];
   if (sName!=nil && sName.length!=0) {
     [stateArray addObject:sName]; 
    }
}

答案 2 :(得分:0)

试试这个

NSMutableArray *countries = [LoginResult objectForKey:@"Response"];

for(NSDictionary *country in countries){

    NSString *countryName = country[@"country_name"];
   if(countryName)
        [countryArray addObject:countryName];

    NSNumber *countryId = country[@"country_id"];
    if(countryId)
        [idcountry addObject:countryId];

    NSString *stateName = country[@"state_name"];
    if(stateName)
        [stateArray addObject:stateName];
}