填充的 NSArray 无意中丢失了其内容

发布于 2024-12-10 18:23:03 字数 1539 浏览 0 评论 0原文

数组已正确填充,但当我尝试再次访问内容时,似乎又是空的!尝试发布尽可能多的代码。感谢您的帮助。

我在.h文件中声明appData:

@interface userViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

    NSArray *appData;

    IBOutlet UIActivityIndicatorView *activityIndicator;
    IBOutlet UILabel *status;
    IBOutlet UITableView *mainTable;
}

@property (nonatomic, retain) NSArray *appData;
@property (nonatomic, retain) UIActivityIndicatorView *activityIndicator;
@property (nonatomic, retain) UILabel *status;
@property (nonatomic, retain) UITableView *mainTable;


- (IBAction) refresca: (id)sender;

- (void)getData: (NSString *)usuari: (NSString *)clau;

@end

在.m文件中被合成并发布。当连接请求结束时,appData 被正确填充,然后,当我重新加载 tableview 时,当执行 numberOfRowsInSection 时(同时循环也是为了测试),appData 为空!

- (void)requestFinished:(ASIHTTPRequest *)request {
    NSLog(@"%@", [request responseString]);

    self.appData = [[request responseString] componentsSeparatedByString: @"#"]; 

    int x =0;

    while (x<[appData count] - 1)
    {
        NSLog(@"Aplicaciones = %@",[appData objectAtIndex: x]);
        x = x+1;
    }

    [activityIndicator stopAnimating];
    activityIndicator.hidden = YES;
    status.hidden = YES;
    mainTable.hidden = NO;

    [mainTable reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{     
    if ([appData count] != 0)
    {
        NSLog(@"Counter = %@",[appData objectAtIndex:0]);
    }

    return [appData count]-1;
}

An array is filled properly but when I try to access to content again, seems that is empty again!! trying to post as many code as necessary. Thanks for help.

I declare appData in .h file:

@interface userViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

    NSArray *appData;

    IBOutlet UIActivityIndicatorView *activityIndicator;
    IBOutlet UILabel *status;
    IBOutlet UITableView *mainTable;
}

@property (nonatomic, retain) NSArray *appData;
@property (nonatomic, retain) UIActivityIndicatorView *activityIndicator;
@property (nonatomic, retain) UILabel *status;
@property (nonatomic, retain) UITableView *mainTable;


- (IBAction) refresca: (id)sender;

- (void)getData: (NSString *)usuari: (NSString *)clau;

@end

in .m file is synthesized and released. appData is properly filled when connection request ends and then, when I reload tableview, when numberOfRowsInSection executed (while loop also in order to test), appData is empty!

- (void)requestFinished:(ASIHTTPRequest *)request {
    NSLog(@"%@", [request responseString]);

    self.appData = [[request responseString] componentsSeparatedByString: @"#"]; 

    int x =0;

    while (x<[appData count] - 1)
    {
        NSLog(@"Aplicaciones = %@",[appData objectAtIndex: x]);
        x = x+1;
    }

    [activityIndicator stopAnimating];
    activityIndicator.hidden = YES;
    status.hidden = YES;
    mainTable.hidden = NO;

    [mainTable reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{     
    if ([appData count] != 0)
    {
        NSLog(@"Counter = %@",[appData objectAtIndex:0]);
    }

    return [appData count]-1;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

居里长安 2024-12-17 18:23:03

这里有几件事。

如果 appdata 的计数不为零,您在 numberOfRows 中的测试日志记录将导致应用挂起。

您确定获得的 appData 对象与您在 requestFinished 中填充的对象相同吗?

我建议在 numberOfRows 中使用访问器,如 [self.appData count] 中那样,这可能会解决问题。

从计数中减一有什么具体原因吗?因为这样你就会从 tableView 的数组中丢失一个元素

A couple of things here.

Your test logging in numberOfRows will cause the app to hang if appdata ever has a non-zero count.

Are you sure you are getting the the same appData object that you populate in requestFinished ?

I suggest using the accessor in numberOfRows as in [self.appData count] which might sort out the problem.

And is there a specific reason you subtract one from the count? As you will lose one element from the array in the tableView that way

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文