我有一个NSMutableArray并向其添加对象,如下所示:
// Do any additional setup after loading the view.
array = [[NSMutableArray alloc] init];
[array addObject:@"Apple"];
[array addObject:@"Orange"];
[array addObject:@"Mango"];
[array addObject:@"Banana"];
但是当我尝试从JSONModelArray添加对象时仍然只显示上面的4个对象。
这是JSONModelArray代码:
//fetch the feed
_collectionBusinessFeeds = [[CollectionBusinessManager alloc] initFromURLWithString:@"http://localhost:8888/json/collectionBusinessFinal.json"
completion:^(JSONModel *model, JSONModelError *err) {
for(NSDictionary *item in _collectionBusinessFeeds.collectionBusinesses){
NSLog(@"%@",item);
NSString *title = [item valueForKey:@"name"];
[array addObject:title];
NSLog(@"%@",array);
// here array is printing all the objects with the above 4 objects
}
}];
但是下面的返回数组只显示4个以上的对象,而不是从JSONModelArray添加的所有对象。
-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [array count];
// only showing 4 above obects like Apple, Banana...
}
这是我的完整实施文件:
#import "CollectionViewController.h"
#import "AccordionViewController.h"
#import "JSONModelLib.h"
#import "CollectionBusinessManager.h"
#import "HUD.h"
@interface CollectionViewController (){
CollectionBusinessManager* _collectionBusinessFeeds;
}
@end
@implementation CollectionViewController
NSMutableArray* array;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
array = [[NSMutableArray alloc] init];
[array addObject:@"Apple"];
[array addObject:@"Orange"];
[array addObject:@"Mango"];
[array addObject:@"Banana"];
//show loader view
[HUD showUIBlockingIndicatorWithText:@"Fetching JSON"];
//fetch the feed
_collectionBusinessFeeds = [[CollectionBusinessManager alloc] initFromURLWithString:@"http://localhost:8888/json/collectionBusinessFinal.json"
completion:^(JSONModel *model, JSONModelError *err) {
//hide the loader view
[HUD hideUIBlockingIndicator];
[self.v reloadData];
NSLog(@"%@", _collectionBusinessFeeds.collectionBusinesses);
for(NSDictionary *item in _collectionBusinessFeeds.collectionBusinesses){
NSLog(@"%@",item);
NSString *title = [item valueForKey:@"name"];
[array addObject:[title copy]];
NSLog(@"%@",array);
}
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark collection view methods
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [array count];
}
- (UIColor*) randomColor{
int r = arc4random() % 255;
int g = arc4random() % 255;
int b = arc4random() % 255;
return [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UILabel *label = (UILabel *) [cell viewWithTag:100];
label.text = [array objectAtIndex:indexPath.row];
cell.backgroundColor = [self randomColor];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
AccordionViewController *detailVC = [[AccordionViewController alloc] init];
detailVC.passParam= [array objectAtIndex:indexPath.row];
detailVC.title = detailVC.passParam;
[self.navigationController pushViewController:detailVC animated:YES];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
任何人都可以帮助我吗?
更新 最后它解决了我添加如下属性的问题:
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
[self.collectionView reloadData];
答案 0 :(得分:0)
//fetch the feed
_collectionBusinessFeeds = [[CollectionBusinessManager alloc] initFromURLWithString:@"http://localhost:8888/json/collectionBusinessFinal.json"
completion:^(JSONModel *model, JSONModelError *err) {
for(NSDictionary *item in _collectionBusinessFeeds.collectionBusinesses){
NSLog(@"%@",item);
NSString *title = [item valueForKey:@"name"];
[array addObject:title];
[collectionView reloadData];
NSLog(@"%@",array);
}
}];
I have edited code, please check above code.
答案 1 :(得分:0)
对于您所描述的内容,似乎在集合视图计算了项目数后调用了fetch。您必须在获取新结果后重新加载集合视图。
答案 2 :(得分:0)
<强>已更新强>
angular.module('app').controller('MyController',['$scope', function($scope){ //$scope in function here
$(document).ready(function () {
var form = $scope.Form1; // undefined
}]);
});
旧答案 如果将__block添加到数组初始化中,它必须有效。
#import "CollectionViewController.h"
#import "AccordionViewController.h"
#import "JSONModelLib.h"
#import "CollectionBusinessManager.h"
#import "HUD.h"
@interface CollectionViewController (){
CollectionBusinessManager* _collectionBusinessFeeds;
}
@property (nonatommic, strong) NSMutableArray *array;
@end
@implementation CollectionViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.array = [[NSMutableArray alloc] init];
[self.array addObject:@"Apple"];
[self.array addObject:@"Orange"];
[self.array addObject:@"Mango"];
[self.array addObject:@"Banana"];
But when I try to add object from JSONModelArray still showing the above 4 object only.
Here is JSONModelArray code:
//fetch the feed
__weak typeof(self) weakSelf = self;
_collectionBusinessFeeds = [[CollectionBusinessManager alloc] initFromURLWithString:@"http://localhost:8888/json/collectionBusinessFinal.json"
completion:^(JSONModel *model, JSONModelError *err) {
for(NSDictionary *item in _collectionBusinessFeeds.collectionBusinesses){
NSLog(@"%@",item);
NSString *title = [item valueForKey:@"name"];
[weakSelf.array addObject:title];
NSLog(@"%@", weakSelf.array);
// here array is printing all the objects with the above 4 objects
}
}];