當按鈕按下加載下面發佈的類時出現錯誤。無法使用標識符標記出列單元格
該代碼應該加載滑出菜單。這是導致我的問題的線。我對iOS/obj-c完全陌生。我不確定爲什麼,但是這行代碼所在的方法是爲_menuItems
數組中的每個條目遍歷?數組中的每個項目的NSLog輸出,但它運行另一次並引發此錯誤?這就是我認爲至少在發生的事情。如果有人能給我一些指點,我會很感激。除此之外,我的項目有兩個目標 - 我不知道爲什麼,我不知道如何,我不知道如何改變它。這可能是問題嗎?
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
控制檯打印輸出是這樣的:
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'
。
#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
它要麼/或:如果一個類或筆尖註冊,那麼你不必檢查cell!= nil。 –
我正在使用的代碼是從它工作的例子中獲得的,所以我不知道爲什麼需要做出這麼大的改變。我在問題中提到我的項目有兩個目標,可能會以某種方式導致這個問題? – Killian
一些解決方法:1.檢查您的NSString * CellIdentifier是否不爲零。使用UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 而不是UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; –