gpt4 book ai didi

ios - 数组在 numberOfRowsInSection 中等于 0

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:42:47 25 4
gpt4 key购买 nike

我有一个 UITableViewController 用于显示本地业务对象列表的名称。我用过这个 UITableViewController以前从 .csv 文件中读取信息,这一切都在正常工作,并在我的 UITableViewController 中完美地显示了公司名称。

但是,我最近将所有这些信息都移到了 Parse.com,现在我正尝试在我当前的 UITableViewController 中读取和显示来自 Parse.com 的信息。

我正在尝试重构代码而不是在这里使用 Parse.com 的教程: https://www.parse.com/docs/ios_guide#ui-tables/iOS

在我的 viewDidLoad 方法中,我调用我自己的方法 [self initializeContent] 执行 PFQuery 来检索适合的对象列表特定条件,然后将对象保存在 NSMutableArray 中。

这执行得很好,我可以看到业务对象正在通过我的 [self initializeContent] 方法中的 NSLogs 检索并保存到 NSMutableArray(数据数组)。但是,当单步执行代码时,我的 dataArraynumberOfSectionsInTableView 方法中为 0。因此,我没有在 UITableViewController 中看到显示的企业名称。

numberOfSectionsInTableView 是否在 viewDidLoad 方法之前执行,这意味着我的 dataArray 将始终为 0?

我已经逐步检查了代码,但无法弄清楚我的问题出在哪里。

下面是我的代码 - 我尽可能多地包含了代码,以帮助确定我的问题出在哪里。

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

BusinessDetailTableViewController.m

#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
{
[super viewDidLoad];

[self initializeArrays];
[self initializeContent];
}

- (void) initializeArrays
{
dataArray = [[NSMutableArray alloc] init];
defaultBusinessArrayMutable = [[NSMutableArray alloc] init];
}

- (void) initializeContent
{
NSLog(@"Class Name: %@", className); // Shows the correct className
[self queryParse:className];
}

- (void) queryParse:(NSString *) className
{
PFQuery *query = [PFQuery queryWithClassName:self.className];
[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 = [[BusinessDetailContent alloc] 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);


[defaultBusinessArrayMutable addObject:businessDetailContent];


NSString *strFromInt = [NSString stringWithFormat:@"%d", tagNumber];

if (!([strFromInt isEqualToString:@"3"] || [strFromInt isEqualToString:@"10"]))
{
[dataArray addObject:defaultBusinessArrayMutable];
NSLog(@"Default Business Array Mutable Count: %i", (int)dataArray.count);
}
}
}
}
else
{
NSLog(@"Error retrieving objects: %@ %@", error, [error userInfo]);
}
}];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@" cellForRowAtIndexPath "); // This is not executed

static NSString *CellIdentifier = @"Cell";
NSLog(@"Cell Identifier: %@", CellIdentifier);

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

// Configure the cell
NSArray *cellArray = [dataArray objectAtIndex:indexPath.section];

BusinessDetailContent *businessDetailContent = [cellArray objectAtIndex:indexPath.row];

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 ");
[self performSegueWithIdentifier:@"businessDetailSegue" sender:self];
}



- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@"numberOfRowsInSection returning: %d", (int)[dataArray count]);
return [dataArray count]; // dataArray = 0
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSMutableArray *array = [dataArray objectAtIndex:section];

return [array count];
}


- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(@"PrepareForSegue: %@", segue.identifier);
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

if ([segue.identifier isEqualToString:@"businessDetailSegue"] && indexPath)
{
NSArray *selectedRowArray = [dataArray objectAtIndex:indexPath.section];

BusinessDetailContent *selectedContent = [selectedRowArray objectAtIndex:indexPath.row];

DetailViewController *detailController = segue.destinationViewController;
detailController.businessDetailContent = selectedContent;
}
}a
@end

编辑:我添加了 [self.tableView reloadData];到我的 findObjectsInBackgroundWithBlock 方法的末尾,正如许多人所建议的那样,现在我正在使用 numberOfSectionsInTableView 方法,但是 dataArray 仍然是 0。看起来我的代码正在执行两次,正如控制台打印所看到的那样?

对于我的 queryParse 方法中的 dataArray,它返回 18 个对象——但应该只有 9 个。从控制台打印 Hotel Array Count 从 2 开始,之后每次递增 2。

我的 numberOfRowsInSection 现在被执行了两次,但在 Parse.com 查询之前。

我完全被这个困住了。

enter image description here

最佳答案

@Hans Moolman,是的,你是对的,你只需要在解析完成后调用 TableView 的重新加载方法。即

- (NSMutableArray *) queryParse:(NSString *) className
{
PFQuery *query = [PFQuery queryWithClassName:self.className];
[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 = [[BusinessDetailContent alloc] 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);


[defaultBusinessArrayMutable addObject:businessDetailContent];


NSString *strFromInt = [NSString stringWithFormat:@"%d", tagNumber];

if (!([strFromInt isEqualToString:@"3"] || [strFromInt isEqualToString:@"10"]))
{
[dataArray addObject:defaultBusinessArrayMutable];
NSLog(@"Default Business Array Mutable Count: %i", (int)dataArray.count);
}
}
}
}
else
{
NSLog(@"Error retrieving objects: %@ %@", error, [error userInfo]);
}
}];
// This line to be added
[self.tableView reloadData];

}

如果仍然存在问题,请告诉我。谢谢。

关于ios - 数组在 numberOfRowsInSection 中等于 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24672643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com