Iphone UITableView-滚动到下一页后,多个部分会重复(使用解析)

Iphone UITableView-滚动到下一页后,多个部分会重复(使用解析),iphone,objective-c,ios,uitableview,parse-platform,Iphone,Objective C,Ios,Uitableview,Parse Platform,我正在使用带有自定义UITableViewCell的故事板,它在PFQueryTableViewController中显示四个图像(哪个子类UITableView) 加载第一个页面时,它会正确显示前24个对象。当第二个页面加载时,它重复第一个页面中的前24个对象,并追加下一批24个对象。在第三个页面上,它将重复第一个页面的24个对象与第三个页面的24个对象一起附加到第二个页面的24个对象 如果您能帮助我修改代码,以便在滚动时在适当的页面上显示正确的对象集,我将不胜感激 我使用的是Parse,这个

我正在使用带有自定义
UITableViewCell
的故事板,它在
PFQueryTableViewController
中显示四个图像(哪个子类
UITableView

加载第一个页面时,它会正确显示前24个对象。当第二个页面加载时,它重复第一个页面中的前24个对象,并追加下一批24个对象。在第三个页面上,它将重复第一个页面的24个对象与第三个页面的24个对象一起附加到第二个页面的24个对象

如果您能帮助我修改代码,以便在滚动时在适当的页面上显示正确的对象集,我将不胜感激

我使用的是
Parse
,这个子类是
PFQueryTableViewController
。因为我每行显示四张照片(对象),所以我创建了一个名为
groupedObjectIds
的数组,其中包含包含四个对象的数组

这是
.h
文件:

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <Parse/Parse.h>
@interface TestTableViewController : PFQueryTableViewController <CLLocationManagerDelegate>

@property (strong, nonatomic) NSNumber *institutionID;
@property (nonatomic, retain) CLLocationManager *locationManager;

- (IBAction)insertCurrentLocation:(id)sender;
@end
#import "TestTableViewController.h"

@interface TestTableViewController ()
@end

@implementation TestTableViewController
NSMutableArray *groupedObjectIds;
@synthesize locationManager = _locationManager;
@synthesize institutionID;

- (id)initWithCoder:(NSCoder *)aCoder    
{        
    self = [super initWithCoder:aCoder];

    if (self)            
    {        
        // Customize the table            
        // The className to query on            
        self.className = @"profilePhoto";

        // The key of the PFObject to display in the label of the default cell style            
        self.textKey = @"text";

        // Whether the built-in pull-to-refresh is enabled            
        self.pullToRefreshEnabled = YES;

        // Whether the built-in pagination is enabled            
        self.paginationEnabled = YES;

        // The number of objects to show per page            
        self.objectsPerPage = 24;

        //self.shouldReloadOnAppear = NO;            
    }        
    return self;        
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

   if (scrollView.contentSize.height - scrollView.contentOffset.y < (self.view.bounds.size.height)) {
      if (![self isLoading]) {
         NSLog(@"pulling next page");
         //groupedObjectIds = [[NSMutableArray alloc] init];
         [self loadNextPage];
      }
   }
}

#pragma mark - UIViewController
- (void)viewDidUnload
{
   [super viewDidUnload];
   [self setInstitutionID:nil];

   // Release any retained subviews of the main view.
   // e.g. self.myOutlet = nil;
}

#pragma mark - UIViewController

- (void)viewDidLoad {
   [super viewDidLoad];

   NSLog(@"entered viewdidload");
   groupedObjectIds = [[NSMutableArray alloc] init];
   if (![CLLocationManager locationServicesEnabled]) {            
   }

   //[[self locationManager] startUpdatingLocation];

   // Listen for annotation updates. Triggers a refresh whenever an annotation is dragged and dropped.
   //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadObjects) name:@"geoPointAnnotiationUpdated" object:nil];
}

- (void)viewWillAppear:(BOOL)animated {
   [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
   [super viewDidAppear:animated];

   //if (self.shouldReloadOnAppear) {
   //    self.shouldReloadOnAppear = NO;
   //    [self loadObjects];
   //}
}

- (void)viewWillDisappear:(BOOL)animated {
   [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
   [super viewDidDisappear:animated];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {        
   if (indexPath.section == self.objects.count/4) {
      // this behavior is normally handled by PFQueryTableViewController, but we are using sections for each object and we must handle this ourselves
      UITableViewCell *cell = [self tableView:tableView cellForNextPageAtIndexPath:indexPath];
      return cell;
   }
   else{
      //NSLog(@"index path is: %@", indexPath);
      NSLog(@"indexpath.section is: %d", indexPath.section);
      // A date formatter for the creation date.    
      UITableViewCell *cell = (PFTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ImageCell"];

      if (cell == nil) {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: @"ImageCell"];
          //[cell.photoButton addTarget:self action:@selector(didTapOnPhotoAction:) forControlEvents:UIControlEventTouchUpInside];
      }
      UIImageView *img1 = (UIImageView *)[cell viewWithTag:1000];
      UIImageView *img2 = (UIImageView *)[cell viewWithTag:1001];
      UIImageView *img3 = (UIImageView *)[cell viewWithTag:1002];
      UIImageView *img4 = (UIImageView *)[cell viewWithTag:1003];
      NSLog(@"groupedobjectids in cellforrow count is: %d", groupedObjectIds.count);
      //if([groupedObjectIds objectAtIndex:indexPath.row])
      //if(indexPath.row < groupedObjectIds.count)
      //{
      NSMutableArray *newArray = [[NSMutableArray alloc] init];
      [newArray  addObjectsFromArray:[groupedObjectIds objectAtIndex:indexPath.section]];
      //NSLog(@"newArray count is: %d", newArray.count);
      //NSLog(@"object at index 0 is: %@", [newArray objectAtIndex:0]);
      if([newArray objectAtIndex:0])
      {
         NSString *objectID = [newArray objectAtIndex:0];
         for(PFObject *object in self.objects)
         {
            if([objectID isEqualToString: [object objectId]])
            {
               PFFile *imageFile = [object objectForKey:@"thumbnail"];
               NSData *imageData = [imageFile getData];
               UIImage *selectedPhoto = [UIImage imageWithData: imageData];
               img1.image = selectedPhoto;
            }
         }
      }
      if([newArray objectAtIndex:1])
      {
         NSString *objectID = [newArray objectAtIndex:1];
         for(PFObject *object in self.objects)
         {
            if([objectID isEqualToString: [object objectId]])
            {
               PFFile *imageFile = [object objectForKey:@"thumbnail"];
               NSData *imageData = [imageFile getData];
               UIImage *selectedPhoto = [UIImage imageWithData: imageData];
               img2.image = selectedPhoto;
            }
         }
      }
      if([newArray objectAtIndex:2])
      {
         NSString *objectID = [newArray objectAtIndex:2];
         for(PFObject *object in self.objects)
         {
            if([objectID isEqualToString: [object objectId]])
            {
               PFFile *imageFile = [object objectForKey:@"thumbnail"];
               NSData *imageData = [imageFile getData];
               UIImage *selectedPhoto = [UIImage imageWithData: imageData];
               img3.image = selectedPhoto;
            }
         }
      }
      if([newArray objectAtIndex:3])
      {
         NSString *objectID = [newArray objectAtIndex:3];
         for(PFObject *object in self.objects)
         {
            if([objectID isEqualToString: [object objectId]])
            {
               PFFile *imageFile = [object objectForKey:@"thumbnail"];
               NSData *imageData = [imageFile getData];
               UIImage *selectedPhoto = [UIImage imageWithData: imageData];
               img4.image = selectedPhoto;
            }
         }
      }
      //}        
      return cell;
   }
}

#pragma mark - PFQueryTableViewController

- (void)objectsWillLoad {
   [super objectsWillLoad];

   // This method is called before a PFQuery is fired to get more objects
}    

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


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {        
   int j = self.objects.count / 4;
   NSLog(@"number of sections in tableview: %d", j);
   return j;
}

- (void)objectsDidLoad:(NSError *)error {
   [super objectsDidLoad:error];
   //NSLog(@"inside objects did load");
   NSMutableArray *arrayOfFour = [[NSMutableArray alloc] init];

   int i = 1;
   NSLog(@"self objects count is: %d", self.objects.count);
   for (PFObject *eachObject in self.objects) {            
      NSLog(@"object ID is: %@", [eachObject objectId]);
      [arrayOfFour addObject:[eachObject objectId]];
      if(i%4==0)
      {
         //NSLog(@"mod 4 is 0");
         [groupedObjectIds addObject: arrayOfFour];
         //[arrayOfFour removeAllObjects];
         //arrayOfFour = nil;
         arrayOfFour = [[NSMutableArray alloc] init];
      }
      //NSLog(@"i is: %d", i);
      i++;
   }
   NSMutableArray *newArrayOfFour = [[NSMutableArray alloc] init];
   //[newArrayOfFour addObjectsFromArray:[groupedObjectIds objectAtIndex:0]];
   newArrayOfFour = [groupedObjectIds objectAtIndex:0];
   //NSLog(@"newarrayoffour count is: %d", newArrayOfFour.count);
   for(int i = 0; i<newArrayOfFour.count; i++)
   {
      //NSLog(@"object id here is: %@", [newArrayOfFour objectAtIndex:i]);
   }
   //NSLog(@"groupedObjectIds count in objectsdidload is: %d", groupedObjectIds.count);
   // This method is called every time objects are loaded from Parse via the PFQuery
}

- (PFQuery *)queryForTable {
   /*if (![PFUser currentUser]) {
      PFQuery *query = [PFQuery queryWithClassName:self.className];
      [query setLimit:0];
      return query;
   }*/
   PFQuery *query = [PFQuery queryWithClassName:@"profilePhoto"];        
   [query orderByAscending:@"createdAt"];        
   return query;
}

似乎您有一个可重用单元标识符问题,请尝试在tableviewcontroller方法
cellForRowAtIndexpath:(NSIndexpath*)indexPath
中为已分配的自定义单元设置该标识符,以便控制器不必为列表中的每个对象分配单元

我也有类似的问题。事实证明,在为我使用的表指定查询时:

query.cachePolicy = PFCachePolicy.CacheThanNetwork
而是使用:

query.cachePolicy = PFCachePolicy.CacheElseNetwork
它首先从缓存中加载对象。我已经在同一台设备上运行了几次代码,因此找到并附加了对象。之后,查询从网络请求新对象,它们完全相同,并且这些对象也被附加到tableView中。也许你也有同样的问题