3

I've got several columns in my app where 1 column is of the type array. I want to get the items in that array in ONLY that row into my UITableView, but I have some problems as the numberOfRows only get 1 because there's only 1 row, but I want to count the array of that row instead of the rows themselves.

How can I accomplish this with a PFQueryTableViewController?

Here is some of my "experimenting code" with this:

#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 isn't of much use, but the goal is to get all the array items of 1 row into a UITableView - the question is, how?

UPDATE

My "new" code gives me this error:

    Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

Here is the corresponding 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 items in the array gets downloaded:

enter image description here enter image description here enter image description here

Thanks! Erik

1 Answer 1

1

There are a few options, but this is the easiest in my opinion.

1) Create a new member variable, call it myObjects:

NSArray *myObjects; 

2) Override objectsDidLoad:

- (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) Overload objectAtIndexPath:

- (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 custom 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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I've chosen the "Add exception breakpoint" but I just get to this screen - see my updated question
oh, I see at the top it says cellForRowAtIndexPath, perhaps the indexPath.row is the problem?
Any ideas? Problem is driving me crazy, last little piece of the puzzle ;) @Dehli

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.