2014-11-12 3 views
0

UITableViewUISearchController이 있고 내 맞춤 개체로 가득합니다. 내가 어떤 객체를 찾을 때 나는 reloadData 메서드를 호출한다. 그러나 나는 항상 잃어 버렸기 때문에 나는 어떤 물건도 볼 수 없다. 나는 섹션으로 번역되는 그룹을 가지고, 각 그룹은 섹션의 행으로 번역되는 오브젝트를 포함합니다.내 물건을 잃을 때

UISearchControllerDelegate 메소드 구현 :

일부 코드 및 로그있다

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController 
{ 
    // Searching 

    // Put all found objects into findedObjects 
    // Sort findedObjects to groups and put them into findedGroups 

    findedObjects = searchResults; 

    [findedGroups removeAllObjects]; 

    for (InfoGroup *buffGroup in groups) 
    { 
     InfoGroup *newGroup = [InfoGroup groupWithName:buffGroup.groupName andObjects:nil isMainGroup:buffGroup.mainGroup]; 
     newGroup.groupImageName = buffGroup.groupImageName; 
     newGroup.groupPeoples = buffGroup.groupPeoples; 

     BOOL flag = NO; 

     for (InfoObject *obj in findedObjects) //sort objects 
     { 
      if ([obj.objectGroup.groupName isEqualToString:newGroup.groupName]) 
      { 
       [newGroup.groupObjects addObject:obj]; 
//    obj.objectGroup = newGroup; 
       NSLog(@"count of groupObjects: %li",[newGroup.groupObjects count]); 
       flag = YES; 
      } 
     } 

     if (flag) // if new group have more than 0 objects, put her in array 
     { 
      [findedGroups addObject:newGroup]; 
     } 
    } 

    NSLog(@"found %li groups and summary %li objects", [findedGroups count],[findedObjects count]); 

    [self.tableView reloadData]; 
} 

UITableView 데이터 소스 방법

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    NSInteger count = ([findedGroups count] > 0) ? [findedGroups count] : [groups count]; 

    NSLog(@"number of sections: %li", count); 

    return count; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSInteger count = ([findedGroups count] > 0) ? [[findedGroups[section] groupObjects] count] : [[groups[section] groupObjects] count]; 

    NSLog(@"number of rows: %li in section: %li", count, section); 

    return count; 
} 

매우 흥미로운 기록이 있습니다 (검색 창에 뭔가를 입력하는 동안, 청소) :

found 2 groups and summary 5 objects <----- 5 objects 
number of sections: 2 <----- HERE TWO 
number of rows: 0 in section: 1 <----- here 0 objects 
number of rows: 0 in section: 0 <----- here 0 too 

found 1 groups and summary 1 objects 
number of sections: 1 
number of rows: 0 in section: 0 

found 1 groups and summary 1 objects 
number of sections: 1 
number of rows: 0 in section: 0 

그래서 내 물건을 numberOfSectionsInTableViewnumberOfRowsInSection 사이에서 잃어 버립니다.

제안 사항? 나는 뭔가를 놓칠 수도 있지만, 나는 무엇을 찾을 수 없습니다.

죄송합니다. 정말 어리석은 실수는 시간 낭비입니다. 그냥 마크 게시물에 대한

+0

당신은 배열에 무엇이 표시해야합니다 (즉, 어떤 종류의) 그들이 어떻게 채워집니다. – trojanfoe

+0

업데이트 된 게시물 @trojanfoe 살펴보기 –

답변

0

내가 문제를 해결

를 해결한다. 내가 여기

InfoGroup *newGroup = [InfoGroup groupWithName:buffGroup.groupName andObjects:nil isMainGroup:buffGroup.mainGroup]; 
groupObjects array was not initialized. 

를 전무를 넣어 때문에 그래서 난 그냥 그 경우에 대한 방법을 업데이트하고 있습니다 :

+(instancetype)groupWithName:(NSString*)initialName andObjects:(NSMutableArray*)initialObjects isMainGroup:(BOOL)initialMain 
{ 
    if (initialObjects) 
    { 
     [ig setGroupObjects:initialObjects]; 
    } else 
    { 
     ig.groupObjects = [NSMutableArray array]; 
    } 
} 
관련 문제