gpt4 book ai didi

ios - Objective-C 重复解析对象中的行

转载 作者:行者123 更新时间:2023-11-29 12:39:25 24 4
gpt4 key购买 nike

我一直被这个问题困扰了一段时间。基本上我一直在尝试从解析中提取结果列表,然后从关系指针中获取信息。

我已经到了可以将单元格标签设置为标题的地步,但返回的行数似乎相同。

我知道我需要在某处使用 indexPath.row,但我想不出如何使用。

任何帮助将不胜感激:)

   //
// DEMOWatchedGamesTableViewController.m
// ShoutOutz
//
// Created by Craig Turner on 05/08/2014.
// Copyright (c) 2014 Roman Efimov. All rights reserved.
//

#import "DEMOWatchedGamesTableViewController.h"
#import <Parse/Parse.h>

@interface DEMOWatchedGamesTableViewController ()

@end

NSMutableArray *arrayGames;

@implementation DEMOWatchedGamesTableViewController

- (void)viewDidLoad
{
[super viewDidLoad];


// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;

//PFUser *currentUser = [PFUser currentUser];

PFQuery *query = [PFQuery queryWithClassName:@"watched_games"];



// Include the user data with each post
[query includeKey:@"game_id"];


[query findObjectsInBackgroundWithBlock:^(NSArray *postObjects, NSError *error)
{
if(!error){

NSLog(@"myArray: %@", postObjects);


self.myArray = postObjects;

[self.tableView reloadData];


} else {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}

}];


}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.myArray count];
//return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}


NSLog(@"Array: %@", self.myArray);

for (PFObject * postObject in self.myArray) {


[postObject fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error){

PFObject *postAuthor = [object objectForKey:@"game_id"];


NSLog(@"retrieved related Post Author: %@", postAuthor);

cell.textLabel.text = [postAuthor objectForKey:@"gameTitle"];


}];
}

return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/


@end

最佳答案

请记住,tableView:cellForRowAtIndexPath: 方法会为每个需要显示的单元格调用一次,这取决于您在 tableView:numberOfRowsInSection 中提供的值数据源方法,在本例中为 myArray 数组的大小。

因此,根据该原则,您无需为 cellForRowAtIndexPath 方法的每次迭代都遍历数组。此方法的有用之处在于,它通过向您提供 indexPath 让您知道它将创建哪个单元格索引。因此,您可以使用该方法提供的 indexPath 值作为索引,然后像这样在数组中获取适当的实例:

这只是一个基于您现有代码的示例:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}

/**
Why are you printing the array? youll end up printing it as many times as there are instances
in your myArray array. You dont want to do that ;)
NSLog(@"Array: %@", self.myArray);
*/

//Using the indexpath's row value we can go ahead and grab that from your myArray array
PFObject * postObject = [self.myArray objectAtIndex:indexPath.row];

[postObject fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error){
PFObject *postAuthor = [object objectForKey:@"game_id"];
NSLog(@"retrieved related Post Author: %@", postAuthor);
cell.textLabel.text = [postAuthor objectForKey:@"gameTitle"];
}];
return cell;
}

关于ios - Objective-C 重复解析对象中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25391270/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com