Get array from parsing column in UITableView
I have multiple columns in my application where 1 column is an array of types. I want to get the items in this array of ONLY this row into my UITableView, but I have some problems as the numberOfRows only gets 1 because there is only 1 row, but I want to count the array of this row, not the rows themselves.
How can I accomplish this using PFQueryTableViewController?
Here are some of my "experimental codes":
#import "ReviewsViewController.h"
#import "MyManager.h"
@interface ReviewsViewController () {
// Declare variables
int totalCount;
int currentReview;
}
@end
@implementation ReviewsViewController
- (id)initWithCoder:(NSCoder *)aCoder {
self = [super initWithCoder:aCoder];
if (self) {
// Setup currentReview
currentReview = 0;
// The className to query on
self.parseClassName = @"Story";
// The key of the PFObject to display in the label of the default cell stlye
self.textKey = @"objectId";
// Whether the built-in pull-to-refresh is enabled
self.pullToRefreshEnabled = YES;
// Whether the built-in pagination is enabled
self.paginationEnabled = NO;
}
return self;
}
- (PFQuery *)queryForTable {
PFQuery *query = [PFUser query];
query = [PFQuery queryWithClassName:self.parseClassName];
[query whereKey:@"Title" equalTo:[[MyManager sharedManager] selectedStory]];
return query;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// Setup totalCount
totalCount = 1;
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
// Set the delegate(s) and datasource
_reviewsTableView.delegate = self;
_reviewsTableView.dataSource = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of objects in the section
NSArray *reviews = [[NSArray alloc] init];
for (PFObject *object in [self objects]){reviews = (NSArray *)[object objectForKey:@"Reviews"];}
totalCount = reviews.count;
NSLog(@"%@%d", @"count: ", totalCount);
return totalCount;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *simpleTableIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
// Configure the cell
UILabel *reviewLabel = (UILabel *)[cell viewWithTag:10];
NSArray *reviews = [[NSArray alloc] init];
for (PFObject *object in [self objects]){reviews = (NSArray *)[object objectForKey:@"Reviews"];}
reviewLabel.text = reviews[currentReview];
currentReview++; // Increase it for next time
return cell;
}
@end
I have a feeling this code is not very helpful, but the goal is to get all the elements of a 1 row array in a UITableView - the question is how?
UPDATE
My "new" code is giving me this error:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
Here's the relevant code:
#import "ReviewsViewController.h"
#import "MyManager.h"
@interface ReviewsViewController () {
// Declare variables
NSArray *myObjects;
}
@end
@implementation ReviewsViewController
- (id)initWithCoder:(NSCoder *)aCoder {
self = [super initWithCoder:aCoder];
if (self) {
// The className to query on
self.parseClassName = @"Story";
// The key of the PFObject to display in the label of the default cell stlye
self.textKey = @"objectId";
// Whether the built-in pull-to-refresh is enabled
self.pullToRefreshEnabled = YES;
// Whether the built-in pagination is enabled
self.paginationEnabled = NO;
}
return self;
}
- (void)objectsDidLoad:(NSError*)error {
[super objectsDidLoad:error];
// Extract out the desired array and assign it to myObjects
myObjects = [[NSArray alloc] init];
for (PFObject *object in [self objects]){
myObjects = (NSArray*)[object objectForKey:@"Reviews"];
}
[self.tableView reloadData];
}
- (PFQuery *)queryForTable {
PFQuery *query = [PFUser query];
query = [PFQuery queryWithClassName:self.parseClassName];
[query whereKey:@"Title" equalTo:[[MyManager sharedManager] selectedStory]];
return query;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
// Set the delegate(s) and datasource
_reviewsTableView.delegate = self;
_reviewsTableView.dataSource = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return myObjects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath object:(PFObject *)object {
static NSString *simpleTableIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
// Configure the cell using myObjects
UILabel *reviewLabel = (UILabel *)[cell viewWithTag:10];
reviewLabel.text = [myObjects objectAtIndex:indexPath.row];
return cell;
}
@end
Here you can see that both elements in the array are being loaded:
Thank! Erik
source to share
There are several options, but this is the easiest one in my opinion.
1) Create a new member variable, call it myObjects:
NSArray *myObjects;
2) Override DidLoad objects:
- (void)objectsDidLoad:(NSError*)error {
[super objectsDidLoad:error];
// Extract out the desired array and assign it to myObjects
myObjects = [[NSArray alloc] init];
for (PFObject *object in [self objects]){
myObjects = (NSArray*)[object objectForKey:@"Reviews"];
}
[self.tableView reloadData];
}
3) Overloading the AtIndexPath object:
- (PFObject*) objectAtIndexPath:(NSIndexPath*)indexPath {
PFObject *object = [PFObject objectWithClassName: @"Object"];
[object setObject:[myObjects objectAtIndex:indexPath.row] forKey:@"string"];
return object;
}
4) Use myObjects for numberOfRowsInSection:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return myObjects.count;
}
5) Use your own object for cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath object:(PFObject *)object {
static NSString *simpleTableIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
// Configure the cell using object
UILabel *reviewLabel = (UILabel *)[cell viewWithTag:10];
reviewLabel.text = [object objectForKey:@"string"];
return cell;
}
source to share