2013-11-14 17 views
5

聲明失敗由於某種原因,我的應用程序嘗試將單元格加載到tableview時失敗。我在整個應用程序中使用的所有桌面視圖都完全一樣,我對它們沒有任何問題。可能有必要從另一個viewController初始化控制器嗎?我不這麼認爲。我的故事板中的單元格已被賦予標識符。這裏是一個屏幕截圖的鏈接。在 - [UITableView dequeueReusableCellWithIdentifier:forIndexPath:]

enter image description here

所以我已經試過各種here,並沒有奏效。

下面是一個使用輸出[的tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]方法:

2013-11-14 15:36:33.419 ComplyOS[43683:70b] *** Assertion failure in -[UITableView   dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2903.23/UITableView.m:5261 
2013-11-14 15:36:33.424 ComplyOS[43683:70b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier ProcedureCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' 
*** First throw call stack: 
(
0 CoreFoundation      0x0000000101fa9795 __exceptionPreprocess + 165 
1 libobjc.A.dylib      0x0000000101d0c991 objc_exception_throw + 43 
2 CoreFoundation      0x0000000101fa961a +[NSException raise:format:arguments:] + 106 
3 Foundation       0x00000001018a99e9 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 189 
4 UIKit        0x00000001008f9879 -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] + 150 
5 ComplyOS       0x00000001000099b4 -[EndoPxTableViewController tableView:cellForRowAtIndexPath:] + 116 
6 UIKit        0x0000000100902b8a -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 348 
7 UIKit        0x00000001008ea836 -[UITableView _updateVisibleCellsNow:] + 2297 
8 UIKit        0x00000001008fb381 -[UITableView layoutSubviews] + 207 
9 UIKit        0x0000000100892b27 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 354 
10 QuartzCore       0x00000001005afa22 -[CALayer layoutSublayers] + 151 
11 QuartzCore       0x00000001005a4589 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 363 
12 QuartzCore       0x00000001005a440a _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 
13 QuartzCore       0x0000000100519694 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 252 
14 QuartzCore       0x000000010051a70c _ZN2CA11Transaction6commitEv + 394 
15 QuartzCore       0x000000010051ad79 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89 
16 CoreFoundation      0x0000000101f74ff7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 
17 CoreFoundation      0x0000000101f74f67 __CFRunLoopDoObservers + 391 
18 CoreFoundation      0x0000000101f546d2 __CFRunLoopRun + 946 
19 CoreFoundation      0x0000000101f53f33 CFRunLoopRunSpecific + 467 
20 GraphicsServices     0x0000000103c2b3a0 GSEventRunModal + 161 
21 UIKit        0x0000000100837043 UIApplicationMain + 1010 
22 Comply        0x0000000100002703 main + 115 
23 libdyld.dylib      0x00000001029ed5fd start + 1 
24 ???         0x0000000000000001 0x0 + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

這裏是我的UITableView的控制器代碼:

#import "EndoPxTableViewController.h" 
#import <AFHTTPRequestOperationManager.h> 
#import "ProcedureTableViewCell.h" 

@interface EndoPxTableViewController(){} 

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

@implementation EndoPxTableViewController 


- (void)setVisitItem:(Patient_VisitsModel *)visitItem{ 
if(_visitItem !=visitItem){ 
    _visitItem = visitItem; 
    } 
} 

-(void)setUserId:(NSString *)userId{ 
    if(_userId !=userId){ 
     _userId = userId; 
    } 
[self fetchProceduresData]; 
} 


- (void) fetchProceduresData{ 
NSString *procedureURLString =[NSString stringWithFormat:@"https://XXXX"; 


AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
[manager GET:procedureURLString parameters:nil success:^(AFHTTPRequestOperation *operation, id json) { 


    _procedures = json; 
    [self.tableView reloadData]; 

} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Error occured in getSessionID method"); 
    NSLog(@"Error: %@", error); 

}]; 
} 

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

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
self.title = @"Endoscopy Report"; 
} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
NSLog(@"Procedures:%@", [_procedures valueForKey:@"description"]); 
return _procedures.count; 
} 

- (ProcedureTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"ProcedureCell"; 
ProcedureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
NSString *procedureDescription = [_procedures[indexPath.row] valueForKey:@"description"]; 
NSLog(@"Procedure Description:%@", procedureDescription); 


cell.procedureLabel.text = procedureDescription; 
cell.questionImage.image = nil; 

return cell; 
} 

#pragma mark - Navigation 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
} 

@end 

感謝大家的幫助。我有點絕望。

+0

看來您正在爲自定義單元格使用自定義類「ProcedureTableViewCell」。請確保你不是以某種方式取代單元格標識符 –

+0

我在該課程中做的所有事情是爲標籤和UIImage分配IBOutlets。 –

+0

您是否將原型單元的'Class'設置爲'ProcedureTableViewCell'。 'ProcedureTableViewCell'是'UITableViewCell'的一個子類。 –

回答

20

我的不好!它似乎是你正在使用dequeueReusableCellWithIdentifier:forIndexPath:方法出院細胞。請嘗試dequeueReusableCellWithIdentifier:而不是它應該工作。

dequeueReusableCellWithIdentifier:forIndexPath:需要一個nib文件或一個註冊類,用於使單元出列,但是您使用的是故事板原型單元格。如果使用正確的標識符,則dequeueReusableCellWithIdentifier:保證從故事板返回一個單元格。

希望它有幫助。

+0

Tada的代碼....我之前已經嘗試過,它不起作用。我不知道我做了什麼不同,但它用正確的單元格數加載了視圖。現在我必須弄清楚爲什麼不將「procedureDescription」加載到每個單元格中。 NSLog註銷很好... –

+0

你鍵入的內容不起作用 – LKM

+0

@LKM我不確定什麼不能解決你的問題,也不知道你的問題。我認爲這個答案不適合你的問題。謝謝。 –

1

我有和你一樣的問題。 我發現了什麼問題。

在你的代碼,

static NSString *CellIdentifier = @"ProcedureCell"; 
ProcedureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

你應該使用「dequeueReusableCellWithIdentifier:forIndexPath:indexPath」當你在故事板使用視圖控制器之間賽格瑞方法(我說的故事板「連接控制器」)。

如果你只是繪製由使用Interface Builder您的自定義單元格,你不得不用這樣的方法,下面

static NSString *CellIdentifier = @"ProcedureCell"; 
    ProcedureTableViewCell *cell = (ProcedureTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[ProcedureTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
0

試試這個下面的代碼來配置您的自定義單元格。

//configure cell 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *cellIdentifier = @"ProcedureCell"; 

ProcedureTableViewCell *cell = (ProcedureTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

// If there is no cell to reuse, create a new one 
if(cell == nil) { 
    cell = [[ProcedureTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 

} 
return cell; 
}