ios – UITableView reloadData什么都不做(UITableView不是nil)

前端之家收集整理的这篇文章主要介绍了ios – UITableView reloadData什么都不做(UITableView不是nil)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在构建我的第一个真正的应用程序,你们已经是最有帮助的,但我一直在努力与tableView不更新.我希望这里的某个人可以对此有所了解,并且可能想出任何关于我可能错过或完成不必要的其他事情的想法.

目前,应用应该执行以下操作:

>使用来自sqlite db的数据填充表(基本上只列出所有表)
>用户按下按钮(触发fetchData方法)
>(void)fetchData将本地数据库与服务器数据库校验和进行比较
>(void)fetchData设置NSURLConnection,通过POST与服务器通信
>当连接收到所有数据时,它会更新本地数据库
>使用新的db数据更新tableView dataSource(NSMutableArray)
> tableview更新并列出数据库中的所有表[myTableView reloadData]被调用

我经过双重检查的事情:tableview和delegate,datasource和outlet之间的连接; tableView不是零;数据源本身已经准确更新;

一切都达到了最后一点(7).似乎myTableView当时并不是零.想法?如果您需要查看更多我的代码,请告诉我.

我也看了一下beginUpdates和endUpdates的方法,但在我看来,他们一度关注一些变化和用户交互性.我想根据用户选择重新加载整个表(即希望根据当前用户登录反映整个其他sql选择字符串).或者还有另一种更好的方法吗?

提前致谢!

这是一段相当不错的代码

  1. #import "FirstViewController.h"
  2. @interface FirstViewController ()
  3. @end
  4.  
  5. @implementation FirstViewController
  6. #import "FirstViewController.h"
  7.  
  8. @synthesize myTextView,myTableViewDataSource,myFetchedData,resultat,tablesAndChecks,tablesArr,checksArr,tablesToRequest,receivedData,receivedDataString,sql,sqlStatementsArr,FailedsqlStatementsArr,FailedsqlStatementsCodeArr,dbloop1;
  9.  
  10.  
  11.  
  12. #pragma mark Table view methods
  13. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  14. return 1;
  15. }
  16.  
  17. // Customize the number of rows in the table view.
  18. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  19. NSLog(@"Konfigurerar tableView"); //swedish for "Configuring tableView"
  20. if(myTableView.dataSource==nil){
  21. NSLog(@"datasource = nil");
  22. }else{
  23. NSLog(@"datasource != nil"); //This prints in log
  24. }
  25. NSLog(@"%d",[myTableViewDataSource count]); //prints "19" in log
  26.  
  27. return [myTableViewDataSource count];
  28. }
  29.  
  30. // Customize the appearance of table view cells.
  31. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  32. NSLog(@"calling cellForRowAtIndexPath"); //This does NOT print in log
  33.  
  34. static NSString *CellIdentifier = @"myCell";
  35.  
  36. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  37. if (cell == nil) {
  38. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  39. }
  40.  
  41. // Set up the cell...
  42. cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
  43. cell.textLabel.text = [NSString stringWithFormat:@"Tabell %d: %@",[indexPath row],[myTableViewDataSource objectAtIndex:[indexPath row]]];
  44.  
  45. return cell;
  46. }
  47.  
  48.  
  49.  
  50. - (void)connectionDidFinishLoading:(NSURLConnection *)connection
  51. {
  52. NSLog(@"connection did finish loading");
  53.  
  54. //...
  55. //script that receives a long sql-string from the server
  56. //and then updates the database with it goes here
  57. //...
  58.  
  59. //Ok,so now the database has updated correctly,and it's time
  60. //to update the tableview so that it reflects the new data.
  61. //
  62. //Get data from database...
  63. NSMutableArray * tempArray = [[NSMutableArray alloc] initWithCapacity:0];
  64.  
  65. if (sqlite3_open(dbpath,&contactDB) == sqlITE_OK)
  66. {
  67. NSLog(@"Databasen öppnad");
  68. NSString *beginsql = [NSString stringWithFormat: @"SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;"];
  69. const char *begin_stmt = [beginsql UTF8String];
  70. sqlite3_prepare_v2(contactDB,begin_stmt,-1,&statement,NULL);
  71. while(sqlite3_step(statement) == sqlITE_ROW) {
  72. char *col1 = (char *)sqlite3_column_text(statement,0);
  73. if (col1 !=NULL){
  74. [tempArray addObject:[NSString stringWithUTF8String: col1]];
  75. }
  76. }
  77. if (sqlite3_step(statement) == sqlITE_DONE)
  78. {
  79. NSLog(@"#####%s; Done: %@",sqlite3_errmsg(nil),beginsql);
  80. } else {
  81. NSLog(@"#####%s; Error with string: %@; Errcode: %d Errmsg: %s",beginsql,sqlite3_errcode(contactDB),sqlite3_errmsg(nil));
  82. }
  83.  
  84. NSLog(@"%d",sqlite3_finalize(statement));
  85. sqlite3_close(contactDB);
  86.  
  87. //update the datasource to the values of tempArray
  88. myTableViewDataSource = tempArray;
  89.  
  90. //Some logging to see that the updated data is in the array... (which it is)
  91. NSLog(@"myTableViewDataSource count: %d",[myTableViewDataSource count]);
  92.  
  93. for (i=0; i<[myTableViewDataSource count]; i++) {
  94. NSLog(@"%@",[myTableViewDataSource objectAtIndex:i]);
  95. }
  96. }
  97.  
  98. //Finally,check so that myTableView isn't nil and can receive messages
  99. if(myTableView == nil){
  100. NSLog(@"NILCHECK mytableview is nil!");
  101. }else{
  102. NSLog(@"NILCHECK mytableview is NOT nil!"); //This is printed out to the log
  103. }
  104.  
  105. //Reload data -> nothing happens...
  106. [myTableView reloadData];
  107. }
  108.  
  109.  
  110. - (void)fetchData
  111. {
  112. //function that gets current checksums for all db tables on the server,compares them to the local database
  113. //and then requests an sql-string from the server to update the tables that needs it.
  114. //I don't think this is relevant for my problem,and it runs fine anyways.
  115.  
  116. //The last thing it does is setting up a NSURLConnection to communicate with
  117. //the server (sending which tables to request via POST and then getting the sql-string as the server response)
  118.  
  119. NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
  120. if (theConnection) {
  121. //receivedData = [NSMutableData data];
  122. NSLog(@"ReceivedData är: %d",[receivedData length]);
  123. } else {
  124. NSLog(@"Connection Failed!");
  125. }
  126.  
  127. //All done here,now the ReceivedData method takes over
  128.  
  129. }
  130. }

更新:

这是我的viewDidLoad方法

  1. - (void)viewDidLoad
  2. {
  3. //creates database and fills "tempArray" with data
  4.  
  5. NSLog(@"%d",sqlite3_finalize(statement));
  6. sqlite3_close(contactDB);
  7.  
  8. myTableViewDataSource = tempArray;
  9. NSLog(@"myTableViewDataSource count: %d",[myTableViewDataSource objectAtIndex:i]);
  10. }
  11.  
  12. }
  13.  
  14. myTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
  15. myTableView.delegate = self; //Added this after comment (1)
  16. myTableView.dataSource = self; //Added this after comment (2)
  17. }

更新:
添加后:

  1. myTableView.delegate = self;
  2. myTableView.dataSource = self;

to viewDidLoad,tableView:numberOfRowsInSection:被调用,而dataSource不是nil,但是没有调用tableView:cellForRowAtIndexPath:,并且表没有被更新.

更新2:
这是我的头文件

  1. //
  2. // FirstViewController.h
  3. // OHBSYS Storyboards
  4. //
  5. // Created by David Forsberg on 2012-09-25.
  6. // Copyright (c) 2012 David Forsberg. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10. #import <sqlite3.h>
  11.  
  12. @interface FirstViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>{
  13. NSString *databasePath;
  14. sqlite3 *contactDB;
  15.  
  16. UITextView *myTextView;
  17. UITableView *myTableView;
  18.  
  19. NSMutableArray *myTableViewDataSource;
  20.  
  21. NSMutableString * tableRowCount;
  22. NSMutableArray * dbloop1;
  23.  
  24. //And a bunch of other variables here
  25. }
  26.  
  27. @property (retain,nonatomic) IBOutlet UITextView *myTextView;
  28. @property (nonatomic,retain) IBOutlet UITableView *myTableView;
  29. @property (nonatomic,retain) IBOutlet NSMutableArray *myTableViewDataSource;
  30.  
  31. - (IBAction) fetchData;
  32.  
  33. @property (nonatomic) NSString * tableRowCount;
  34. @property (nonatomic,retain) NSMutableArray * dbloop1;
  35.  
  36. //A bunch of other properties here as well
  37.  
  38. @end

更新3:

我已经尝试使用断点检查值.

tableview内的断点:numberofrowsinsection:

  1. (lldb) po self.myTableView
  2. (UITableView *) $0 = 0x0d13ca00 <UITableView: 0xd13ca00; frame = (0 20; 768 1004); clipsToBounds = YES; layer = <CALayer: 0x2a3d30>; contentOffset: {0,0}>
  3. (lldb) po self.myTableView.delegate
  4. (objc_object *) $1 = 0x0ce99500 <FirstViewController: 0xce99500>
  5. (lldb) po self.myTableView.dataSource
  6. (objc_object *) $2 = 0x0ce99500 <FirstViewController: 0xce99500>
  7. (lldb) po self.myTableViewDataSource
  8. (NSMutableArray *) $3 = 0x0029fb20 <__NSArrayM 0x29fb20>(
  9. CONTACTS,sqlite_sequence
  10. )
  11.  
  12. (lldb)

数据库更新后的断点,就在调用reloadData之前:

  1. (lldb) po self.myTableView
  2. (UITableView *) $4 = 0x0d13ca00 <UITableView: 0xd13ca00; frame = (0 20; 768 1004); clipsToBounds = YES; layer = <CALayer: 0x2a3d30>; contentOffset: {0,0}>
  3. (lldb) po self.myTableView.delegate
  4. (objc_object *) $5 = 0x0ce99500 <FirstViewController: 0xce99500>
  5. (lldb) po self.myTableView.dataSource
  6. (objc_object *) $6 = 0x0ce99500 <FirstViewController: 0xce99500>
  7. (lldb) po self.myTableView.dataSource //(accidentally hit that twice)
  8. (objc_object *) $7 = 0x0ce99500 <FirstViewController: 0xce99500>
  9. (lldb) po self.myTableViewDataSource
  10. (NSMutableArray *) $8 = 0x0ce6baa0 <__NSArrayM 0xce6baa0>(
  11. CONTACTS,Meta_tablechecksums,prot_multicoltest,prot_multicoltest_4,prot_multicoltest_4_desc,prot_multicoltest_desc,sqlite_sequence,superadmin_filefolders,superadmin_files,superadmin_imagefolders,superadmin_images,sys_customers,sys_fieldlooks,sys_fieldtypes,sys_formtables,sys_pages,sys_subpages,sys_userroles,sys_users
  12. )
  13.  
  14. (lldb)

断点AT reloadData行:

  1. (lldb) po self.myTableView
  2. (UITableView *) $9 = 0x0d13ca00 <UITableView: 0xd13ca00; frame = (0 20; 768 1004); clipsToBounds = YES; layer = <CALayer: 0x2a3d30>; contentOffset: {0,0}>
  3. (lldb) po self.myTableView.delegate
  4. (objc_object *) $10 = 0x0ce99500 <FirstViewController: 0xce99500>
  5. (lldb) po self.myTableView.dataSource
  6. (objc_object *) $11 = 0x0ce99500 <FirstViewController: 0xce99500>
  7. (lldb) po self.myTableViewDataSource
  8. (NSMutableArray *) $12 = 0x0ce6baa0 <__NSArrayM 0xce6baa0>(
  9. CONTACTS,sys_users
  10. )
  11.  
  12. (lldb)

tableview中的断点:numberofrowsinsection:在更新db并运行reloadData命令之后:

  1. (lldb) po self.myTableView
  2. (UITableView *) $13 = 0x0d13ca00 <UITableView: 0xd13ca00; frame = (0 20; 768 1004); clipsToBounds = YES; layer = <CALayer: 0x2a3d30>; contentOffset: {0,0}>
  3. (lldb) po self.myTableView.delegate
  4. (objc_object *) $14 = 0x0ce99500 <FirstViewController: 0xce99500>
  5. (lldb) po self.myTableView.dataSource
  6. (objc_object *) $15 = 0x0ce99500 <FirstViewController: 0xce99500>
  7. (lldb) po self.myTableViewDataSource
  8. (NSMutableArray *) $16 = 0x0ce6baa0 <__NSArrayM 0xce6baa0>(
  9. CONTACTS,sys_users
  10. )
  11.  
  12. (lldb)

解决方法

我没有看到myTableView被合成.
在执行reloadData时,还调用了tableView数据源方法吗?

如果没有,请仔细检查并验证myTableView委托是否设置正确.
如果您已正确地将表连接到笔尖并且您有一个插座,您也可以在代码中设置它,即在viewDidLoad方法中,通过设置:

  1. myTableView.delegate = self;

猜你在找的iOS相关文章