2017-11-22 1 views
0

UITable 및 탐색 컨트롤러가있는 기본보기 컨트롤러 (SecondViewController)가 있습니다. 탐색 모음 단추를 누르면 테이블 상단의 탐색 줄에서 메뉴가 드롭 다운됩니다. 이 메뉴는 다음처럼 하위 뷰 같은 뷰 컨트롤러를 추가함으로써 생성된다 :메인 스레드에서 호출 된 리로드로도 UITable이 새로 고침되지 않습니다.

//SecondViewController.m 
self = sortMenu.secondVC; 
[self addChildViewController:sortMenu]; 
[self.view addSubview:sortMenu.view]; 
[sortMenu didMoveToParentViewController:self]; 

sortMenu는 세포 메인 뷰 컨트롤러 클래스 메소드를 호출하여 표시 순서를 변경하는 버튼을 포함한다. sortButtonPressed에서

//SortMenuViewController.m 
- (IBAction)sortButtonPressed:(id)sender { 
    [_secondVC sortButtonPressed:[sender tag]]; 

} 

, 그것은 업데이트 종류의 필터 값을 페치 요청을하는 방법을 호출합니다.

//SecondViewController.m 
-(void)sortButtonPressed:(NSInteger)sortDescriptor{ 
    _sortDescriptor = sortDescriptor; 
    currentPredicate = [NSPredicate predicateWithFormat:@"dataset & %d > 0", 4]; 
    [self fetchResultsUsingSegmentedControlIndex]; 
} 

가져 오기 요청이 수행되고 새 순서로 데이터가 반환됩니다.

//SecondViewController.m 
- (IBAction)fetchResultsUsingSegmentedControlIndex 
{ 
    NSString* sectionNameKeyPath = nil; 
    NSArray* sortDescriptors = nil; 
    NSSortDescriptor *scientificNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"scientificName" ascending:YES]; 
    NSSortDescriptor *commonNameFirstDescriptor = [[NSSortDescriptor alloc] initWithKey:@"commonNameFirst" ascending:YES]; 
    NSSortDescriptor *commonNameLastDescriptor = [[NSSortDescriptor alloc] 
                initWithKey:@"commonNameLast" 
                ascending:YES 
                selector:@selector(localizedCaseInsensitiveCompare:)]; 

    if (_sortDescriptor == kSortByCommonNameFirst) 
    { 
     sortDescriptors = [[NSArray alloc] initWithObjects:commonNameFirstDescriptor, commonNameLastDescriptor, scientificNameDescriptor, nil]; 
     sectionNameKeyPath = @"commonNameFirst"; 
    } 
    else if (_sortDescriptor == kSortByCommonNameLast) 
    { 
     sortDescriptors = [[NSArray alloc] initWithObjects:commonNameLastDescriptor, commonNameFirstDescriptor, scientificNameDescriptor, nil]; 
     sectionNameKeyPath = @"commonNameLast"; 
    } 
    else if (_sortDescriptor == kSortByScientificName) 
    { 
     sortDescriptors = [[NSArray alloc] initWithObjects:scientificNameDescriptor, commonNameFirstDescriptor, commonNameLastDescriptor, nil]; 
     sectionNameKeyPath = @"scientificName"; 
    } 

    NSError *error; 

    NSLog(@"current predicate: %@", currentPredicate); 

    [[self fetchedResultsControllerWithsectionNameKeyPath:sectionNameKeyPath sortDescriptors:sortDescriptors predicate:currentPredicate] performFetch:&error]; 


    [scientificNameDescriptor release]; 
    [commonNameLastDescriptor release]; 
    [commonNameFirstDescriptor release]; 
    [sortDescriptors release]; 

    NSUInteger sectionsCt = [[speciesFetchedResultsController sections] count]; 
    int sum = 0; 
    for (int i=1; i < sectionsCt; i++){ 
     id <NSFetchedResultsSectionInfo> sectionInfo = [[speciesFetchedResultsController sections] objectAtIndex:i]; 
     NSUInteger numOfObj = [sectionInfo numberOfObjects]; 
     NSLog(@" in section %d number of objects is %lu ", i, (unsigned long)numOfObj); 
     sum = sum + numOfObj; 
    } 


    [_table performSelectorOnMainThread:@selector(reloadData) 
            withObject:nil 
            waitUntilDone:NO]; 
} 

내가 메인 뷰 컨트롤러에서 fetchResultsUsingSegmentedControlIndex를 호출

(정렬 메뉴를 삭제하기 전에이)가 올바르게 작동합니다. 그러나 sortMenu에서 호출 된 경우 numberOfRowsInSection, numberOfSectionsInTableViewcellForRowAtIndexPath은 호출되지 않습니다. 나는 performSelectorOnMainThread와 함께 메인 스레드에서 reloadData을 호출하려고 시도했으며 메인 큐에 디스패치했지만 어느 것도 작동하지 않습니다.

원래 탐색보기 버튼을 누르면 기본보기 컨트롤러에 선택기를 추가하여 정렬 메뉴를 만들었고 테이블을 올바르게 다시로드했습니다. 메뉴를위한 별도의 뷰 컨트롤러를 생성 한 이후 (더 큰 디자인 제어 기능을 가짐) 작동하지 않습니다.

+0

'[_secondVC sortButtonPressed : [보낸 사람 태그]];'... 어떻게'_secondVC'를 설정하고 있습니까? – DonMag

+0

@DonMag updated post - 고마워요! – Matt

+0

'self = sortMenu.secondVC;'???? 그래서 당신은 당신의'sortMenu' 뷰 컨트롤러를 인스턴스화했고, 다른 뷰 컨트롤러에서는'sortMenu'의'secondVC' ***에'self'를 할당합니까? 그리고 self에 자식 뷰 컨트롤러로'sortMenu'를 추가합니다. sortMenu의 secondVC는 셀프 등등에 추가됩니다 ... 어떤 의미를 가지지 않는 것 같습니다 ... – DonMag

답변

0

위임을 사용하여 종료되었습니다.

// SortMenuViewController.h 

#import <UIKit/UIKit.h> 

@protocol SortMenuViewControllerDelegate <NSObject> 
-(void)sortButtonPressed:(NSInteger)sortDescriptor; 
-(void)viewButtonPressed:(NSInteger)viewDescriptor; 
@end 

@interface SortMenuViewController : UIViewController{ 

} 

//SortMenuViewController.m 
- (IBAction)changeSort:(id)sender { 

    [_delegate sortButtonPressed:[sender tag]]; 

} 

//SecondViewController.h 
@interface SecondViewController : UIViewController <NSFetchedResultsControllerDelegate, SortMenuViewControllerDelegate>{ 

} 
-(void)sortButtonPressed:(NSInteger)sortDescriptor{ 
    _sortDescriptor = sortDescriptor; 
    currentPredicate = [NSPredicate predicateWithFormat:@"dataset & %d > 0", dataset]; 
    [self fetchResultsUsingSegmentedControlIndex]; 
} 
관련 문제