我认为这可能是一个简单的解决方案,但我很难找到合适的解决方案 我有以下内容:
ContactList - 使用UICollectionView
方法加载内容的viewDidAppear:
。当在集合视图中选择一个项目时,我激发了这个方法:
[self performSegueWithIdentifier:@"pushContactDetail" sender:self];
到目前为止一切顺利。一切都很好。但是,当我使用导航控制器按钮从ContactDetail页面导航回来时,我的ContactList会复制内容。
我是否应该采取措施阻止viewDidAppear:
再次投放?
当我推送ContactDetail页面时,我认为我不想将集合设置为nil,因为这会导致每次重新加载内容...
以下是代码:
-(void) viewDidAppear
{
[super viewDidAppear];
[self.view setBackgroundColor:myColorLightGrey];
_contactList = [[NSMutableArray alloc] init];
[self displayLoadingAndDisableTableViewInteractions];
[self queryData];
}
- (void) queryData
{
//Find the Data
PFQuery *query = [PFUser query];
PFUser *consumer = [PFUser currentUser];
[query includeKey:User_followers];
[query whereKey:@"email" equalTo:consumer.email];
query.maxCacheAge = 60 * 60 * 24; // One day, in seconds
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
for (PFUser *tmpConsumer in objects)
{
for (PFUser *publisher in [tmpConsumer objectForKey:User_followers])
{
[_contactList addObject:publisher];
}
}
[_collectionView reloadData];
[self hideLoadingAndEnableTableViewInteractions];
}];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"contactCell";
ContactCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
//SNIPPED REMAINDER OF CODE
...
}
答案 0 :(得分:2)
在queryData
方法中,首先清除数组
_contactList = nil;
_contactList = [NSMutableArray array]; // or [[NSMutableArray alloc] init];
像这样移动_contactList
的alloc / init方法:
- (void) queryData
{
_contactList = nil;
_contactList = [[NSMutablArray alloc] init];
//Find the Data
PFQuery *query = [PFUser query];
PFUser *consumer = [PFUser currentUser];
[query includeKey:User_followers];
[query whereKey:@"email" equalTo:consumer.email];
query.maxCacheAge = 60 * 60 * 24; // One day, in seconds
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
for (PFUser *tmpConsumer in objects)
{
for (PFUser *publisher in [tmpConsumer objectForKey:User_followers])
{
[_contactList addObject:publisher];
}
}
[_collectionView reloadData];
[self hideLoadingAndEnableTableViewInteractions];
}];
}
答案 1 :(得分:-2)
使用Animation true导航回来。它会解决你的问题。