gpt4 book ai didi

ios - 带有 UICollectionView 的 NSFetchedResultsController 在更新和删除操作时出现索引/单元格问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:30:00 25 4
gpt4 key购买 nike

我第一次使用 UICollectionView,遇到了一些困难。尤其是更新和删除单元格(这里将重点放在删除上,希望是来的原因),数据来自 NSFetchResultController。我在界面生成器中制作了一个自定义单元格作为 Storyboard的一部分,如下所示:

enter image description here

我有一个具有以下属性的自定义 UICollectionViewCell 子类:

@property (strong, nonatomic) IBOutlet UIButton *deleteButton;
@property (strong, nonatomic) IBOutlet UITextField *textField;
@property (strong, nonatomic) IBOutlet UIView *containerView;
@property (strong, nonatomic) IBOutlet UIView *textFieldContainer;

在 IB 中,我已将单元类设置为我的自定义类,将元素连接到我的自定义类的属性并将标识符设置为 Cell

在我的 Collection View View Controller 中,我设置了 collection view 和 fetchResultController 以及相关的方法,如下所示:

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return [[self.fetchedResultsController sections] count];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
return [sectionInfo numberOfObjects];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NoteCollectionViewCell* cell = (NoteCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}

- (void)configureCell:(NoteCollectionViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
cell.deleteButton.tag = indexPath.row;
[cell.deleteButton addTarget:self action:@selector(deleteNote:) forControlEvents:UIControlEventTouchUpInside];

[...]

// I'm having some weird problem with this, se description below.
Note *note = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textField.tag = indexPath.row;
cell.textField.delegate = self;
cell.textField.text = note.name;
[...]

#pragma mark - Fetched results controller

- (NSFetchedResultsController *)fetchedResultsController
{
[Default FRC method]
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
UICollectionView *collectionView = self.collectionView;

switch(type) {
case NSFetchedResultsChangeInsert:
[collectionView insertItemsAtIndexPaths:@[newIndexPath]];
break;

case NSFetchedResultsChangeDelete:
[collectionView deleteItemsAtIndexPaths:@[indexPath]];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:(NoteCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath] atIndexPath:indexPath];
break;
}
}

我的删除操作如下所示:

- (void)deleteNote:(UIButton *)sender
{
Note *note = [self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForItem:sender.tag inSection:0]];
[[AppDelegate sharedAppDelegate].managedObjectContext deleteObject:note];
[[AppDelegate sharedAppDelegate] saveContext];
}

我的更新操作(UITextField 委托(delegate)方法)如下所示:

- (void)textFieldDidEndEditing:(UITextField *)textField
{
Note *note = [self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForItem:textField.tag inSection:0]];
note.name = textField.text;
[[AppDelegate sharedAppDelegate] saveContext];
}

问题如下:

  • 删除按钮并不总能删除正确的单元格/对象。有时对象/单元格根本不会被删除。
  • 有时,当我删除一个对象时,应用程序会崩溃(SIGARBT 错误)。
  • 有时文本会出现在错误的文本字段中。
  • 有时,当我删除带有一些文本的第 0 行,然后添加按添加时,会添加一个新单元格,其文本值与前一个(已删除)单元格相同。

任何关于如何解决这些问题的想法都会很棒!

更新

正如下面的评论,我的 deleteNote 操作中的这个日志总是为 indexPath 行返回 0。不知道这是否是原因。

NSIndexPath *indexPath = [NSIndexPath indexPathForItem:sender.tag inSection:0];
NSLog(@"indexPath: %@. Row: %d", indexPath, indexPath.row);

最佳答案

您不能以与 TableView 完全相同的方式将获取的结果 Controller 链接到 Collection View 。获取结果 Controller 专门设计用于在核心数据和 TableView 之间进行调解,因此委托(delegate)方法与 TableView 的工作方式相关联。

主要区别在于 TableView 有一个beginUpdates/endUpdates 方法对,您可以将所有更新包装在其中。 Collection View 没有这个,您必须自己构建所有必需的更新调用,然后当获取的结果 Controller 完成更新时,在 performBatchUpdates:completion: 调用中执行它们。

有这个 on GitHub 的示例实现.

关于ios - 带有 UICollectionView 的 NSFetchedResultsController 在更新和删除操作时出现索引/单元格问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18159462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com