填充的 NSArray 无意中丢失了其内容
数组已正确填充,但当我尝试再次访问内容时,似乎又是空的!尝试发布尽可能多的代码。感谢您的帮助。
我在.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有几件事。
如果 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 inrequestFinished
?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