我正在使用自定义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;
}
答案 0 :(得分:0)
好像你有一个可重复使用的单元标识符问题,尝试在tableviewcontroller方法cellForRowAtIndexpath:(NSIndexpath*)indexPath
中设置你用故事板创建的已分配自定义单元格,这样控制器就不必为每个单元格分配一个单元格列表中的对象!
答案 1 :(得分:0)
我有类似的问题。事实证明,在为我使用的表格指定查询时:
query.cachePolicy = PFCachePolicy.CacheThanNetwork
改为使用:
query.cachePolicy = PFCachePolicy.CacheElseNetwork
它首先从缓存中加载对象。我已经多次在同一个设备上运行代码,因此找到并附加了对象。之后,查询从网络请求了新对象,它们完全相同,并且这些对象也附加到tableView。也许你正在解决同样的问题。