2016-05-18 4 views
0

ドキュメントディレクトリにあるファイルのファイル名をテーブルビューに読み込もうとしていますが、ボタンをクリックしてテーブルビューを表示すると、インデックスのオブジェクトが0であるが、配列内には14個のオブジェクトが存在するというエラーが発生します。何が原因だろう?TableViewにドキュメントディレクトリのファイルの名前を設定する空の配列を持つ例外

@implementation loadDataTVC 

- (void)viewDidLoad { 
[super viewDidLoad]; 



} 

-(void) viewWillAppear:(BOOL)animated{ 
DataSource *sharedManager = [DataSource sharedManager]; 

NSString *documentDirectoryPath =   [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]; 
sharedManager.directoryContent = [[NSFileManager defaultManager]  contentsOfDirectoryAtPath:documentDirectoryPath error:NULL]; 
NSLog(@"%lu", (unsigned long)[sharedManager.directoryContent count]); 
} 

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

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
DataSource *sharedManager = [DataSource sharedManager]; 

NSString *documentDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]; 
sharedManager.directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentDirectoryPath error:NULL]; 

if (sharedManager.directoryContent.count > 0) { 
    return (unsigned long)[sharedManager.directoryContent count]; 
} 
else{ 
    return 1; 
} 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"files" forIndexPath:indexPath]; 
DataSource *sharedManager = [DataSource sharedManager]; 
NSString *documentDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]; 
sharedManager.directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentDirectoryPath error:NULL]; 

if (sharedManager.directoryContent.count > 0) { 
    cell.textLabel.text = [NSString stringWithFormat:@"%@", [sharedManager.directoryContent objectAtIndex:indexPath.row]]; 
} 
else{ 
    cell.textLabel.text = @"No data Available"; 

} 
    return cell; 
} 

ログ:

2016-05-18 15:00:44.502 GeoData Grapher[4554:331076] 14 
2016-05-18 15:00:47.412 GeoData Grapher[4554:331076] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray' 
*** First throw call stack: 
(
0 CoreFoundation      0x000000010d2cdd85 __exceptionPreprocess + 165 
1 libobjc.A.dylib      0x000000010cd41deb objc_exception_throw + 48 
2 CoreFoundation      0x000000010d275885 -[__NSArray0 objectAtIndex:] + 101 
3 GeoData Grapher      0x000000010a5f07d6 -[DeviceListTVC prepareForSegue:sender:] + 326 
4 UIKit        0x000000010bcc95d5 -[UIStoryboardSegueTemplate _performWithDestinationViewController:sender:] + 369 
5 UIKit        0x000000010bcc9433 -[UIStoryboardSegueTemplate _perform:] + 82 
6 UIKit        0x000000010bcc96f7 -[UIStoryboardSegueTemplate perform:] + 156 
7 UIKit        0x000000010b583a8d -[UIApplication sendAction:to:from:forEvent:] + 92 
8 UIKit        0x000000010b991067 -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 152 
9 UIKit        0x000000010b583a8d -[UIApplication sendAction:to:from:forEvent:] + 92 
10 UIKit        0x000000010b6f6e67 -[UIControl sendAction:to:forEvent:] + 67 
11 UIKit        0x000000010b6f7143 -[UIControl _sendActionsForEvents:withEvent:] + 327 
12 UIKit        0x000000010b6f72be -[UIControl _sendActionsForEvents:withEvent:] + 706 
13 UIKit        0x000000010b6f6263 -[UIControl touchesEnded:withEvent:] + 601 
14 UIKit        0x000000010b5f699f -[UIWindow _sendTouchesForEvent:] + 835 
15 UIKit        0x000000010b5f76d4 -[UIWindow sendEvent:] + 865 
16 UIKit        0x000000010b5a2dc6 -[UIApplication sendEvent:] + 263 
17 UIKit        0x000000010b57c553 _UIApplicationHandleEventQueue + 6660 
18 CoreFoundation      0x000000010d1f3301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 
19 CoreFoundation      0x000000010d1e922c __CFRunLoopDoSources0 + 556 
20 CoreFoundation      0x000000010d1e86e3 __CFRunLoopRun + 867 
21 CoreFoundation      0x000000010d1e80f8 CFRunLoopRunSpecific + 488 
22 GraphicsServices     0x000000010eb7aad2 GSEventRunModal + 161 
23 UIKit        0x000000010b581f09 UIApplicationMain + 171 
24 GeoData Grapher      0x000000010a5f405f main + 111 
25 libdyld.dylib      0x000000011253c92d start + 1 
26 ???         0x0000000000000001 0x0 + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
(lldb) 

セグエ:

+0

これは、配列がnilであることを意味します。ブレークポイントを設定するか、nslogを使用してチェックしてください。 – Lion

+2

'3 GeoData Grapher 0x000000010a5f07d6 - [DeviceListTVC prepareForSegue:sender:] + 326'が問題と思われます - DecipeListTVCの' prepareForSegue'ではどうしますか? – luk2302

+0

@Lion NSlogの結果が上部(14)に表示されます。したがって、配列は空ではありません – Wilbe

答えて

0
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
NSIndexPath *selectedPath = [self.tableView indexPathForCell:sender]; 
AirconsoleDevice *selectedDevice = self.manager.defaultDevice; 
if (selectedPath.section == 0) { 
    selectedDevice = self.currentDevices[selectedPath.row]; 
} 
if ([segue.identifier isEqualToString:@"session_details"]) { 
    AirconsoleSession *session = [[AirconsoleSession alloc] initWithDevice:selectedDevice]; 
    currentSession = session; 
    SessionDetailsVC *sdvc = segue.destinationViewController; 
    sdvc.session = session; 

} else { 
    // we are presenting the Device Details 
    DeviceDetailsTVC *ddvc = [segue.destinationViewController viewControllers][0]; 
    ddvc.device = selectedDevice; 
    } 
} 

問題が宛先コントローラがこの行に

DeviceDetailsTVC *ddvc = [segue.destinationViewController viewControllers][0]; ddvc.device = selectedDevice;

あります私はナビゲーションコントローラを想定している。スタック内の最初のコントローラにアクセスしています。 これが実際にストーリーボードに正しく設定されていることを確認してください。これは、コード行が期待していることだからですが、クラッシュではviewControllers配列内にメンバーが見つかりません。 AFAIKテーブルビューとは関係なく14項目あります。

+0

あなたは正しいです、それはsdkと共に提供されているサンプルアプリケーションの一部だったので、segueは必要ありませんでした。それはもはや必要とされない別のテーブルビューコントローラを指していました。 iOSでの私の知識が少なからず言葉に制限されているので、問題を取り上げてくれてありがとう。 – Wilbe

+0

デバッグログは非常に初心者/ユーザーフレンドリーではありませんが、ログに直接2つの重要な手掛かりがあります。 1)クラッシュの理由を見る - >インデックス0は配列の境界を超えています。もちろん、配列が空の場合は、インデックスが0の単一のメンバーはありません 2)これはどこで発生するのかを見つける必要があります - >スタックトレースを見て - ボトムからトップへ.... [DeviceListTVC prepareForSegue:sender:] + 326 の直後に、空の配列の問題が発生しました...これは、問題がprepareForSegueMethodにローカライズされていることを示しています。それから、それは正しい行を見つけており、私たちは完了しています。 –

関連する問題