【发布时间】:2015-12-01 11:52:11
【问题描述】:
当我使用 tableview 解析 json 时,如果有 json 项目,但如果没有加载 json 项目,并且当我点击返回按钮时,一切都会好起来。
erminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:
我认为当我单击快速后退按钮并在我的表格视图代码下给出此错误时,json 不会加载。
@interface MasterViewController ()
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, assign) NSInteger totalPages;
@property (nonatomic, assign) NSInteger totalItems;
@property (nonatomic, assign) NSInteger maxPages;
@property (nonatomic, strong) NSMutableArray *activePhotos;
@property (strong, nonatomic) NSMutableArray *staticDataSource;
@property (nonatomic, strong) NSMutableArray *searchResults;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
self.activePhotos = [[NSMutableArray alloc] init];
self.searchResults = [[NSMutableArray alloc] init];
self.staticDataSource = [[NSMutableArray alloc] init];
}
#pragma mark - Table View
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.activePhotos.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (indexPath.row == [self.activePhotos count]) {
cell = [self.tableView dequeueReusableCellWithIdentifier:@"LoadingCell" forIndexPath:indexPath];
UIActivityIndicatorView *activityIndicator = (UIActivityIndicatorView *)[cell.contentView viewWithTag:100];
[activityIndicator startAnimating];
} else {
NSDictionary *photoItem = self.activePhotos[indexPath.row];
cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = [photoItem objectForKey:@"name"];
if (![[photoItem objectForKey:@"description"] isEqual:[NSNull null]]) {
cell.detailTextLabel.text = [photoItem objectForKey:@"description"];
}
}
return cell;
}
- (void)loadPhotos:(NSInteger)page
{
NSString *userismim =[[NSUserDefaults standardUserDefaults] stringForKey:@"userisim"];
NSArray* words = [userismim componentsSeparatedByCharactersInSet :[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString* nospacestring = [words componentsJoinedByString:@""];
NSLog(@"%@",nospacestring);
NSString *apiURL = [NSString stringWithFormat:@"http://bla.com/server/table.php?user=%@",nospacestring];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:apiURL]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
if (!error) {
NSError *jsonError = nil;
NSMutableDictionary *jsonObject = (NSMutableDictionary *)[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];
NSLog(@"%@",jsonObject);
[self.staticDataSource addObjectsFromArray:[jsonObject objectForKey:@"photos"]];
self.currentPage = [[jsonObject objectForKey:@"current_page"] integerValue];
self.totalPages = [[jsonObject objectForKey:@"total_pages"] integerValue];
self.totalItems = [[jsonObject objectForKey:@"total_items"] integerValue];
self.activePhotos = self.staticDataSource;
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}
}] resume];
}
感谢一切。我需要你的帮助。
【问题讨论】:
-
tableView:numberOfRowsInSection:在哪里? -
@TimurBernikowich 添加底部
-
如我所见,您的代码中有很多问题。首先,您的 NSURLSessionTask 具有指向 ViewController 的强指针。这就是为什么当你返回时它仍然会重新加载。
-
在
NSDictionary *photoItem = self.activePhotos[indexPath.row];线上添加断点并检查我们为什么要去那里。 -
添加界面代码置顶