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

发布时间: 2017/3/28 20:45:09
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我已经被困在这个问题上一段时间现在。基本上我一直拉从解析结果的列表,然后从关联的指针获取信息。

我有一点我可以将单元格标签设置为标题,但它似乎有相同的标题,如返回的行数。

我知道我需要利用 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

解决方法 1:

记住, 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;
}
赞助商