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

标签 iphone objective-c ios uitableview parse-platform

我正在使用带有自定义 UITableViewCell 的 Storyboard 在 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;
}

最佳答案

好像你有一个可重用的单元格标识符问题,尝试在 tableviewcontroller 方法中设置它 cellForRowAtIndexpath:(NSIndexpath*)indexPath对于您使用 Storyboard 创建的已分配自定义单元格,因此 Controller 不必为列表中的每个对象分配一个单元格!

关于iphone - UITableView - 滚动到下一页后,多个部分会重复(使用 Parse),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13654113/

相关文章:

iPhone 浏览器为一些 DIV 添加了右边距

iphone - 使用Oauth2成功认证后获取用户信息

ios - 你如何在 XCode 中使用组?

iphone - 如何更新 NSMutableDictionary?

iphone - 覆盖单个单元格或部分的 tableView.separatorStyle

iphone - 如何查找 Gmail 和 Facebook 帐户的所有事件 session ?

iphone - iAd 委托(delegate)不起作用。

objective-c - 如何对 NSMutableArray 进行排序?需要代码审查

ios - SwiftUI |选择器 - 更改所选行的颜色

iPhone、UINavigationController 和 UITabBar 如何在不考虑所选选项卡的情况下呈现模态对话框?