从解析列中获取数组并将其放入UITableView

3

我的应用程序中有几列,其中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

我有一种感觉这段代码并没有太大用处,但目标是将一行中的所有数组项放入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

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

输入图像描述 输入图像描述 输入图像描述

谢谢! Erik

1个回答

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;
}

我选择了“添加异常断点”,但我只能看到这个屏幕 - 请参见我的更新问题。 - Erik
哦,我看到顶部写着cellForRowAtIndexPath,也许indexPath.row是问题所在? - Erik
有什么想法吗?这个问题让我疯了,只差最后一块拼图了 ;) @Dehli - Erik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接