2013-11-28 3 views
0

아래에 게시 된 클래스를 버튼 누름으로로드 할 때 오류가 발생합니다.식별자 태그가있는 셀을 큐에서 빼낼 수 없습니다.

코드는 슬라이드 아웃 메뉴를로드해야합니다. 이것은 내 문제를 일으키는 라인입니다. 나는 iOS/obj-c에 완전히 익숙하다. 이유는 모르겠지만 코드의이 줄은 _menuItems 배열의 각 항목에 대해 반복됩니다. NSLog는 배열의 각 항목을 출력하지만 다른 시간에 실행되어이 오류가 발생합니다. 적어도 내가 생각하는 것보다. 누군가가 나에게 몇 가지 조언을 해줄 수 있다면 감사 할 것입니다. 모든 것을 제외하고, 내 프로젝트는 두 가지 목표를 가지고 있습니다. 왜 그런지 모르겠고, 어떻게해야할지 모르겠고, 어떻게 변경해야할지 모르겠다. 그게 문제일까요?

013-11-28 12:11:31.902 DatabaseTest[57858:a0b] The code runs through here! 
2013-11-28 12:11:31.908 DatabaseTest[57858:a0b] The code runs through here! 
2013-11-28 12:11:31.911 DatabaseTest[57858:a0b] The code runs through here! 
2013-11-28 12:11:31.917 DatabaseTest[57858:a0b] The code runs through here! 
2013-11-28 12:11:31.921 DatabaseTest[57858:a0b] The code runs through here! 
2013-11-28 12:11:31.924 DatabaseTest[57858:a0b] The code runs through here! 
2013-11-28 12:11:31.929 DatabaseTest[57858:a0b] The code runs through here! 
2013-11-28 12:11:31.931 DatabaseTest[57858:a0b] The code runs through here! 
2013-11-28 12:11:31.932 DatabaseTest[57858:a0b] *** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2903.23/UITableView.m:5261 
2013-11-28 12:11:31.936 DatabaseTest[57858:a0b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier tag - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' 

:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

콘솔 출력이 있습니다.

#import "SidebarViewController.h" 
#import "SWRevealViewController.h" 

@interface SidebarViewController() 

@property (nonatomic, strong) NSArray *menuItems; 
@end 

@implementation SidebarViewController 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.view.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f]; 
    self.tableView.backgroundColor = [UIColor colorWithWhite:0.2f alpha:1.0f]; 
    self.tableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f]; 

    _menuItems = @[@"title", @"news", @"comments", @"map", @"calendar", @"wishlist", @"bookmark", @"tag"]; 

} 

- (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (id) sender 
{ 
    // Set the title of navigation bar by using the menu items 
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
    UINavigationController *destViewController = (UINavigationController*)segue.destinationViewController; 
    destViewController.title = [[_menuItems objectAtIndex:indexPath.row] capitalizedString]; 


    if ([segue isKindOfClass: [SWRevealViewControllerSegue class]]) { 
     SWRevealViewControllerSegue *swSegue = (SWRevealViewControllerSegue*) segue; 

     swSegue.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc) { 

      UINavigationController* navController = (UINavigationController*)self.revealViewController.frontViewController; 
      [navController setViewControllers: @[dvc] animated: NO ]; 
      [self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES]; 
     }; 

    } 

} 
- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [self.menuItems count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *CellIdentifier = [self.menuItems objectAtIndex:indexPath.row]; 
    NSLog(@"The code runs through here!"); 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    return cell; 
} 

@end 

답변

1

당신은 사용해야합니다

static NSString *CellIdentifier = @"messageCell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
셀은 셀 ID와 클래스 또는 펜촉을 등록하지 않은 경우

는 사용하지 마십시오 ;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier 
forIndexPath:indexPath]; 

애플 문서 :이 메소드를 호출하기 전에 방법 : forCellReuseIdentifier : 나있는 registerClass : forCellReuseIdentifier

당신은 registerNib를 사용하여 클래스 나 펜촉 파일을 등록해야합니다

중요.

편집 :이 잘못

+0

그 중 하나/또는 : 클래스 또는 펜촉 인 경우 등록하면 셀을 확인하지 않아도됩니다! = nil. –

+0

내가 사용하고있는 코드는 작동하는 예제에서 가져온 것이므로, 왜 그렇게 과감한 변경이 필요한지 알 수 없습니다. 질문에서 프로젝트에 어떻게 든 두 가지 목표가 있다고 언급했는데,이 문제를 어떻게 든 유발할 수 있습니까? – Killian

+0

해결 방법 : 1. NSString * CellIdentifier가 nil이 아닌지 확인하십시오. 사용 UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier : CellIdentifier]; 대신 UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier : CellIdentifier forIndexPath : indexPath]; –

0

:

이 작동하지 않을 각 행에 셀을 등록하지 않는 한
NSString *CellIdentifier = [self.menuItems objectAtIndex:indexPath.row]; 

.

코드 또는 storyboard/xib에 등록 된 셀 식별자를 사용해야합니다.

또한 각 셀이 고유 한 식별자로 셀을로드하는 경우이 방법으로 셀 재사용의 이점을 얻지 못합니다.

+0

셀 식별자 란 셀의 속성 검사기에서 식별자를 의미합니까? 테이블에 8 개의 프로토 타입 셀이 있으며 각 셀에는 배열의 항목에 해당하는 이름이 있습니다. 하나의 프로토 타입과 배열의 항목을 기반으로 테이블을 채우는 또 다른 예제가 있지만이 정확한 코드를 사용하는 자습서에서 복사하는 순간에는 왜 작동하지 않는지 알 수는 없습니다 – Killian

+0

정적 테이블 뷰를 대신 사용하지 않는 이유는 무엇입니까? – Abizern

+0

저는 완전히 iOS에 익숙해졌습니다. 그래서 저는이 정확한 코드를 사용하는 튜토리얼을 작성하는 예제를 얻으려고합니다. 내가 배운 것을 보면, TableView는 이론 상으로는 하나의 프로토 타입 셀을 가지고있을 뿐이며 배열의 요소 수에 따라 셀이 동적으로 추가됩니다. 정적 테이블 뷰는 필자가 추측 한 동적 부분을 제거합니다. – Killian

0

이 질문에 대한 답을 찾은 후에도 확신이 없지만 여기에있는 경우 문제를 해결할 수있는 방법입니다.

셀에 식별자가 없거나 식별자가있는 경우 식별자가 NSArray * menuItems의 코드와 일치하지 않기 때문에 문제가 발생합니다. 문제를 해결하기 위해 :

  • 당신이 문서 개요로 이동해야합니다 (스토리 보드의 왼쪽 아래에있는 작은 화살표)
  • 다음은 '표보기 셀을 클릭한다 - 태그를. (그것은 당신의 경우 '태그'에, '식별자'

  • 마지막으로, 변경 또는 광고 식별자 이름을 셀 '태그'특성 관리자에

  • 이동을 선택하고 찾아 내야한다.

맞춤법 등의 목적 C주의 트릭을 할해야

  • 잇 짜잔!, 당신이 당신이, 당신이해야 식별자를 주문 같은 방법을해야 의미, 철자에 아주 꽉 코드에서 동일한 철자를 사용하십시오. 도움이

    희망,

    이 가

    참조 사진 : (나는 당신을 위해 하나를 게시 싶었지만 그렇게 할 수없는 것처럼 어머, 보인다!)

  • 관련 문제