I am using RestKit to pull a news feed and it works for pulling the feed. I have used a Master Detail view and it shows multiple feeds in the console but only displays 1 feed in the UITableView (Only 1 cell)
here is my code
@interface MasterViewController (){
NSArray *feedObjects;
NSMutableArray *feedArray;
}
@property NSMutableArray *objects;
@end
@implementation MasterViewController
- (void)awakeFromNib {
[super awakeFromNib];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
self.clearsSelectionOnViewWillAppear = NO;
self.preferredContentSize = CGSizeMake(320.0, 600.0);
}
}
- (void)viewDidLoad {
[super viewDidLoad];
feedArray = [[NSMutableArray alloc]init];
[self configureRestKit];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(RKResponseDescriptor *)responseDescriptor {
RKObjectMapping *bodyMapping = [RKObjectMapping mappingForClass:[BaseClass class]];
[bodyMapping addAttributeMappingsFromArray:@[@"body",@"title"]];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:bodyMapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
return responseDescriptor;
}
-(void)configureRestKit{
NSURL *baseURL = [NSURL URLWithString:@"http://urlExample"];
NSURLRequest *request = [NSURLRequest requestWithURL:baseURL];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[[self responseDescriptor]]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
feedObjects = mappingResult.array;
[feedArray addObject:feedObjects];
[self.tableView reloadData];
RKLogInfo(@"Load collection of Feeds: %@", mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Operation failed with error: %@", error);
}];
[objectRequestOperation start];
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return feedArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSMutableArray *feedarray = feedArray[indexPath.row];
BaseClass *feedO = feedarray[0];
cell.textLabel.text = [NSString stringWithFormat:@"%@",feedO.title];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
}
If anyone could help me that would be great, i've tried adding the mappingResults to a NSArray instead of NSMutableArray but this breaks the program the way i have tried.
Thanks
Of course it does - you are adding only one object to your array here:
[feedArray addObject:feedObjects];
Which means that your function returning number of rows:
feedArray.count == 1
What you want to do is to add all results separately to your NSMutableArray or even use NSArray you got from response directly as data source - so you have an NSArray with only 1 depth level.
[feedArray addObjectsFromArray:feedObjects]
Actually in your case I don't see a reason to keep both feedObjects and feedArray, you can throw away feedArray and use feedObjects instead everywhere.