ios - 数组在 numberOfRowsInSection 中等于 0
<p><p>我有一个 <code>UITableViewController</code> 用于显示本地业务对象列表的名称。我用过这个 <code>UITableViewController</code>
以前从 .csv 文件中读取信息,这一切都正常工作,并在我的 <code>UITableViewController</code> 中完美地显示了业务名称。</p>
<p>但是,我最近已将所有这些信息移至 Parse.com,现在我正在尝试在我当前的 <code>UITableViewController</code> 中读取和显示来自 Parse.com 的信息。</p>
<p>我正在尝试重构代码,而不是在这里使用 Parse.com 的教程:
<a href="https://www.parse.com/docs/ios_guide#ui-tables/iOS" rel="noreferrer noopener nofollow">https://www.parse.com/docs/ios_guide#ui-tables/iOS</a> </p>
<p>在我的 <code>viewDidLoad</code> 方法中,我调用自己的方法 <code></code> 执行 <code>PFQuery</code> 以检索适合的对象列表一定的标准,然后将对象保存在 <code>NSMutableArray</code> 中。</p>
<p>这执行得很好,我可以看到业务对象正在通过我的 <code></code> 方法中的 <code>NSLogs</code> 检索并保存到 <code>NSMutableArray</code>(数据数组)。
但是,在单步执行代码时,我的 <code>dataArray</code> 在 <code>numberOfSectionsInTableView</code> 方法中为 0。因此,我没有看到 <code>UITableViewController</code> 中显示的业务名称。</p>
<p><code>numberOfSectionsInTableView</code> 是否在 <code>viewDidLoad</code> 方法之前执行,这意味着我的 <code>dataArray</code> 将始终为 0?</p>
<p>我已单步执行代码,但无法弄清楚我的问题出在哪里。</p>
<p>以下是我的代码 - 我已尽可能多地包含在内,以帮助确定我的问题所在。</p>
<pre><code>BusinessDetailTableViewController.h
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
@interface BusinessDetailTableViewController : UITableViewController
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *defaultBusinessArrayMutable;
@property (nonatomic, assign) int tagNumber;
@property (nonatomic, strong) NSString *className;
@end
</code></pre>
<p>BusinessDetailTableViewController.m</p>
<pre><code>#import "BusinessDetailTableViewController.h"
#import "BusinessDetailContent.h"
#import "DetailViewController.h"
#import <Parse/Parse.h>
@interface BusinessDetailTableViewController ()
@end
@implementation BusinessDetailTableViewController
@synthesize dataArray;
@synthesize defaultBusinessArrayMutable;
@synthesize tagNumber;
@synthesize className;
- (void)viewDidLoad
{
;
;
;
}
- (void) initializeArrays
{
dataArray = [ init];
defaultBusinessArrayMutable = [ init];
}
- (void) initializeContent
{
NSLog(@"Class Name: %@", className);// Shows the correct className
;
}
- (void) queryParse:(NSString *) className
{
PFQuery *query = ;
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (!error)
{
NSLog(@"Successfully Retrieved %d Objects.", (int)objects.count); // Shows how many objects are retrieved
for (PFObject *object in objects)
{
BusinessDetailContent *businessDetailContent = [ init];
businessDetailContent.no = object[@"ID"];
businessDetailContent.companyName= object[@"Company_Name"];
businessDetailContent.address = object[@"Address"];
businessDetailContent.tel = object[@"Tel"];
businessDetailContent.email = object[@"Email"];
businessDetailContent.website = object[@"Website"];
businessDetailContent.details = object[@"Details"];
businessDetailContent.imageName = object[@"Image_ID"];
// The information here is retrieved correctly
NSLog(@"BUSINESS DETAIL CONTENT - ID: %@, Company Name: %@, Address: %@, Tel: %@, Email: %@, Website: %@, Details: %@, Image_Name: %@",
businessDetailContent.no, businessDetailContent.companyName, businessDetailContent.address, businessDetailContent.tel,
businessDetailContent.email, businessDetailContent.website, businessDetailContent.details,
businessDetailContent.imageName);
;
NSString *strFromInt = ;
if (!( || ))
{
;
NSLog(@"Default Business Array Mutable Count: %i", (int)dataArray.count);
}
}
}
}
else
{
NSLog(@"Error retrieving objects: %@ %@", error, );
}
}];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@" cellForRowAtIndexPath "); // This is not executed
static NSString *CellIdentifier = @"Cell";
NSLog(@"Cell Identifier: %@", CellIdentifier);
UITableViewCell *cell = ;
if (cell == nil)
{
cell = [ initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell
NSArray *cellArray = ;
BusinessDetailContent *businessDetailContent = ;
cell.textLabel.text = businessDetailContent.companyName;
NSLog(@"Cell Textlabel Text: %@", cell.textLabel.text);
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@" didSelectRowAtIndexPath ");
;
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@"numberOfRowsInSection returning: %d", (int));
return ; // dataArray = 0
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSMutableArray *array = ;
return ;
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(@"PrepareForSegue: %@", segue.identifier);
NSIndexPath *indexPath = ;
if ( && indexPath)
{
NSArray *selectedRowArray = ;
BusinessDetailContent *selectedContent = ;
DetailViewController *detailController = segue.destinationViewController;
detailController.businessDetailContent = selectedContent;
}
}a
@end
</code></pre>
<p><strong>编辑:</strong>我添加了 ;正如许多人所建议的那样,到我的 findObjectsInBackgroundWithBlock 方法结束,现在我正在使用 numberOfSectionsInTableView 方法,但 dataArray 仍然是 0。看起来我的代码执行了两次,可以通过控制台打印看到?</p >
<p>对于我的 queryParse 方法中的 dataArray,它返回 18 个对象 - 但应该只有 9 个。
从控制台打印 Hotel Array Count 从 2 开始,之后每次增加 2。 </p>
<p>我的 numberOfRowsInSection 现在被执行了两次,但在 Parse.com 查询之前。 </p>
<p>我完全被这个困住了。</p>
<p> <img src="/image/RbB5o.png" alt="enter image description here"/> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>@Hans Moolman,是的,你是对的,你只需要在解析完成后调用 table view 的 reload 方法。即。</p>
<pre><code>- (NSMutableArray *) queryParse:(NSString *) className
{
PFQuery *query = ;
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (!error)
{
NSLog(@"Successfully Retrieved %d Objects.", (int)objects.count); // Shows how many objects are retrieved
for (PFObject *object in objects)
{
BusinessDetailContent *businessDetailContent = [ init];
businessDetailContent.no = object[@"ID"];
businessDetailContent.companyName= object[@"Company_Name"];
businessDetailContent.address = object[@"Address"];
businessDetailContent.tel = object[@"Tel"];
businessDetailContent.email = object[@"Email"];
businessDetailContent.website = object[@"Website"];
businessDetailContent.details = object[@"Details"];
businessDetailContent.imageName = object[@"Image_ID"];
// The information here is retrieved correctly
NSLog(@"BUSINESS DETAIL CONTENT - ID: %@, Company Name: %@, Address: %@, Tel: %@, Email: %@, Website: %@, Details: %@, Image_Name: %@",
businessDetailContent.no, businessDetailContent.companyName, businessDetailContent.address, businessDetailContent.tel,
businessDetailContent.email, businessDetailContent.website, businessDetailContent.details,
businessDetailContent.imageName);
;
NSString *strFromInt = ;
if (!( || ))
{
;
NSLog(@"Default Business Array Mutable Count: %i", (int)dataArray.count);
}
}
}
}
else
{
NSLog(@"Error retrieving objects: %@ %@", error, );
}
}];
// This line to be added
;
}
</code></pre>
<p>如果仍然存在问题,请告诉我。谢谢。</p></p>
<p style="font-size: 20px;">关于ios - 数组在 numberOfRowsInSection 中等于 0,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/24672643/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/24672643/
</a>
</p>
页:
[1]