从解析列获取数组到UITableView

时间:2014-10-01 17:46:10

标签: ios objective-c arrays uitableview parse-platform

我的应用程序中有几列,其中1列是类型数组。我想把那个数组中的项只在那行中放到我的UITableView中,但是我有一些问题,因为numberOfRows只得到1因为只有1行,但我想计算那行的数组而不是行本身。

如何使用PFQueryTableViewController完成此操作?

以下是我的一些“实验代码”:

#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

我觉得这段代码用处不大,但目标是将1行的所有数组项都放到UITableView中 - 问题是,怎么做?

更新

我的“新”代码给了我这个错误:

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

以下是相应的代码:

#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

在这里您可以看到数组中的两个项目都已下载:

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

谢谢! 埃里克

1 个答案:

答案 0 :(得分:1)

有几种选择,但在我看来这是最简单的。

1)创建一个新的成员变量,称之为myObjects:

NSArray *myObjects; 

2)覆盖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)重载objectAtIndexPath:

- (PFObject*) objectAtIndexPath:(NSIndexPath*)indexPath {
    PFObject *object = [PFObject objectWithClassName: @"Object"];
    [object setObject:[myObjects objectAtIndex:indexPath.row] forKey:@"string"];
    return object;
}

4)将myObjects用于numberOfRowsInSection:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return myObjects.count;
}

5)将您的自定义对象用于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;
}