2017-07-26 88 views
-1

我想加載從firebase數據庫的數據。我能夠使用塊檢索數據並更新NSArray中的值。我可以打印數據,所以我知道數據可用。當我嘗試重新加載表視圖時,應用程序崩潰。有關代碼的任何建議?TableView崩潰,當我嘗試從一個塊加載數據

- (void)viewDidLoad { 

    [super viewDidLoad]; 
    self.title = @"New Chat"; 

    FetchUsers *instance = [[FetchUsers alloc]init]; 

    [instance userInformationSucessBlock:^(NSDictionary *dictionary) { 

    dispatch_async(dispatch_get_main_queue(), ^{ 

      _nameOfUser = [dictionary objectForKey:@"Name"]; 
      _emailOfUser = [dictionary objectForKey:@"Email"]; 
      [self.tableView reloadData]; 
     }); 
}]; 

} 


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

     return self.nameOfUser.count; 
} 

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ComposeCell"]; 
cell.textLabel.text = self.nameOfUser[indexPath.row]; 
return cell; 
} 

崩潰日誌

2017-07-26 16:39:37.333 ChatApp[2870:141943] -[__NSCFString count]: unrecognized selector sent to instance 0x600000227b40 
2017-07-26 16:39:37.339 ChatApp[2870:141943] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString count]: unrecognized selector sent to instance 0x600000227b40' 
*** First throw call stack: 
() 
0 CoreFoundation      0x00000001095cbb0b __exceptionPreprocess + 171 
1 libobjc.A.dylib      0x0000000109030141 objc_exception_throw + 48 
2 CoreFoundation      0x000000010963b134 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132 
3 CoreFoundation      0x0000000109552840 ___forwarding___ + 1024 
4 CoreFoundation      0x00000001095523b8 _CF_forwarding_prep_0 + 120 
5 ChatApp        0x000000010777ca54 -[ComposeViewController tableView:numberOfRowsInSection:] + 100 
6 UIKit        0x0000000109b64dbb -[UITableView _numberOfRowsInSection:] + 57 
7 UIKit        0x0000000109d9b176 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 2344 
8 UIKit        0x0000000109d9f19f -[UITableViewRowData numberOfRows] + 95 
9 UIKit        0x0000000109b41eb8 -[UITableView noteNumberOfRowsChanged] + 121 
10 UIKit        0x0000000109b41508 -[UITableView reloadData] + 2013 
11 ChatApp        0x000000010777c8f1 __36-[ComposeViewController viewDidLoad]_block_invoke_2 + 65 
12 libdispatch.dylib     0x000000010b35e4a6 _dispatch_call_block_and_release + 12 
13 libdispatch.dylib     0x000000010b38705c _dispatch_client_callout + 8 
14 libdispatch.dylib     0x000000010b36840b _dispatch_main_queue_callback_4CF + 411 
15 CoreFoundation      0x0000000109590909 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9 
16 CoreFoundation      0x0000000109556ae4 __CFRunLoopRun + 2164 
17 CoreFoundation      0x0000000109556016 CFRunLoopRunSpecific + 406 
18 GraphicsServices     0x000000010c498a24 GSEventRunModal + 62 
19 UIKit        0x00000001099ef134 UIApplicationMain + 159 
20 ChatApp        0x000000010777d0cf main + 111 
21 libdyld.dylib      0x000000010b3d365d start + 1 
22 ???         0x0000000000000001 0x0 + 1 
+0

在'[self.tableView reloadData]中調用;''viewDidLoad''是毫無意義的,即使你在主調度隊列中發佈了這個。如果發生異常,您正在執行某些視圖中尚未顯示的內容,我會感到驚訝。 –

+0

任何建議如何我可以得到這個工作? – pradeep

+0

崩潰說什麼? –

回答

0

我認爲這個問題是[字典objectForKey:@ 「名」]是不是一個數組而是一個字符串。所以,當你給它分配_nameOfUser和reloadData時,tableView委託試圖獲得一個NSString的數量,並且不可用,所以它會崩潰。 您需要正確提取數據。

+1

此解決方案工作。我修改了我的代碼以將字符串存儲在MutableArray中,並能夠打印數據。謝謝 !! – pradeep

相關問題