我是iOS中的新手,我有一个问题我解析了一个数组,并在表格中显示有三个部分,每个部分我添加了新行,将其所有子级别添加到层次结构中但由于表重新加载问题,其索引更改并显示错误的结果。这是我的代码
+ (void)getTypesWith:(void (^)(NSArray *, NSError *))completionHandler
{
[ZNetworkManager postDataForBackGround:nil atURL:[ZMappingManager getRequestURLToGetPropertiesTypes] completionHandler:^(NSArray *array, NSError *error)
{
NSMutableArray *typesDictionariesArray =[NSMutableArray array];
NSMutableDictionary* details = [NSMutableDictionary dictionary];
if (!error)
{
NSDictionary *fetchedDictionary = (NSDictionary *) array;
if([fetchedDictionary isKindOfClass:[NSDictionary class]] == NO)
{
[details setValue:@"Fetched dictionary is null" forKey:@"desription"];
completionHandler(nil ,[NSError errorWithDomain:@"MyDomain" code:1 userInfo:details]);
}
else
{
if([[[fetchedDictionary objectForKey:@"meta"] objectForKey:@"status"] isEqualToString:@"200"]){
NSDictionary *data = [fetchedDictionary objectForKey:@"response"];
if([data isKindOfClass:[NSDictionary class]] == NO)
{
[details setValue:@"Fetched dictionary is null" forKey:@"desription"];
completionHandler(nil ,[NSError errorWithDomain:@"MyDomain" code:1 userInfo:details]);
}
else
{
NSArray *allTypes = [data objectForKey:@"type"];
if([allTypes count] == 0)
{
[details setValue:@"Fetched dictionary is null" forKey:@"desription"];
completionHandler(nil ,[NSError errorWithDomain:@"MyDomain" code:1 userInfo:details]);
}
else
{
NSMutableArray *searchTypes = [[NSMutableArray alloc] init];
for (int i=0; i<[allTypes count]; i++)
// for (NSDictionary *typeDic in allTypes)
{
NSDictionary *typeDic = [allTypes objectAtIndex:i];
[typesDictionariesArray addObject:typeDic];
ZZameenType *newType = [[ZZameenType alloc] init];
[newType loadFromDictionary:typeDic];
[searchTypes addObject:newType];
NSArray *arrayforChild = [typeDic objectForKey:@"childs"];
for (int j=0; j<[arrayforChild count]; j++)
// for(NSDictionary *typeChild in arrayforChild)
{
NSDictionary *typeChild = [arrayforChild objectAtIndex:j];
ZZameenType *newChild;
[typesDictionariesArray addObject:typeChild];
newChild = [[ZZameenType alloc] init];
[newChild loadFromDictionary:typeChild];
[searchTypes addObject:newChild];
if(j == 0)
{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSString *combineString = [NSString stringWithFormat:@"All %@",[typeDic objectForKey:@"title_alt2"]];
[dict setObject:combineString forKey:@"title"];
[dict setObject:combineString forKey:@"title_alt2"];
[dict setObject:combineString forKey:@"title_alt1"];
[dict setObject:[typeDic objectForKey:@"type_id"] forKey:@"parent_id"];
[dict setObject:[typeDic objectForKey:@"child_list"] forKey:@"type_id"];
[typesDictionariesArray insertObject:dict atIndex:[typesDictionariesArray count]-1];
newChild = [[ZZameenType alloc] init];
[newChild loadFromDictionary:dict];
[searchTypes insertObject:newChild atIndex:[searchTypes count]-1];
}
}
newType = nil;
}
NSSortDescriptor *typeID_sort = [NSSortDescriptor sortDescriptorWithKey:@"type_id" ascending:YES];
[searchTypes sortUsingDescriptors:[NSArray arrayWithObjects:typeID_sort,nil]];
[ZGlobals saveSearchTypes:typesDictionariesArray];
completionHandler(searchTypes ,nil);
searchTypes = nil;
details = nil;
}
}
}else{
}
}
}
}];
}
的cellForRowAtIndexPath
if(selectionC == nil) {
selectionC=[[[NSBundle mainBundle] loadNibNamed:@"SelectionCell" owner:self options:nil] objectAtIndex:0];
}
KLog(@"view frame is %@",NSStringFromCGRect(self.frame));
KLog(@"table frame is %@",NSStringFromCGRect(tableView.frame));
NSArray *values =[[ZGlobals getPropertTypeSectionsValues] objectAtIndex:indexPath.section];
ZZameenType *type =[values objectAtIndex:indexPath.row];
selectionC.selectionTitle.text = type.title;
selectionC.selectionTitle.textColor = [ZTheme cellValuesColorIPAD];