2017-07-26 92 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

相关问题