2

我正在使用带有自定义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

这是.m文件:

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

2 回答 2

0

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

于 2013-01-14T20:52:44.707 回答
0

I had a similar issue. It turned out that when specifying query for table I used:

query.cachePolicy = PFCachePolicy.CacheThanNetwork

instead use:

query.cachePolicy = PFCachePolicy.CacheElseNetwork

And it was first loading objects from cache. I have run the code on one and the same device several times, so the objects were found and appended. After, the query requested new objects from Network and they were completely the same, and these objects were as well appended to the tableView. Maybe you are heaving the same problem.

于 2016-01-15T01:46:32.110 回答