gpt4 book ai didi

iOS:使用没有核心数据的动画更新表格 View 数据源

转载 作者:行者123 更新时间:2023-12-01 19:08:47 28 4
gpt4 key购买 nike

我想用动画(比如 NSFetchedResultsController)更新表格 View 的数据源,但没有核心数据。

可能有添加的对象,删除的对象,移动的对象等......

是否可以?

最佳答案

看看TLIndexPathTools .它是 NSFetchedResultsController 的替代品除了使用 NSFetchRequest和 Core Data 对象,也适用于包含任何类型数据的普通数组。
它提供了一个标准化的数据模型类TLIndexPathDataModel它可以自动将您的数据组织成部分,并具有许多 API 来简化数据源和委托(delegate)方法的实现。但它的主要作用是在您更新数据模型时自动计算并为您执行动画批量更新。

尝试运行大量示例项目以了解可以做什么。 Shuffle sample project是一个很好的起点:点击 Shuffle 按钮, Collection View 网格会随机重新排列自己的动画。这是 Shuffle View Controller 的完整源代码:

#import "TLCollectionViewController.h"
@interface ShuffleCollectionViewController : TLCollectionViewController <UICollectionViewDelegateFlowLayout>
- (IBAction)shuffle;
@end

#import <QuartzCore/QuartzCore.h>
#import "ShuffleCollectionViewController.h"
#import "TLIndexPathDataModel.h"
#import "UIColor+Hex.h"

#define IDX_TEXT 0
#define IDX_COLOR 1

@implementation ShuffleCollectionViewController

- (void)viewDidLoad
{
[super viewDidLoad];
//initialize the controller with a list data items. To keep it simple, we'll
//just use two element arrays (text and color) for our items.
NSArray *items = @[
@[@"A", [UIColor colorWithHexRGB:0x96D6C1]],
@[@"B", [UIColor colorWithHexRGB:0xD696A3]],
@[@"C", [UIColor colorWithHexRGB:0xFACB96]],
@[@"D", [UIColor colorWithHexRGB:0xFAED96]],
@[@"E", [UIColor colorWithHexRGB:0x96FAC3]],
@[@"F", [UIColor colorWithHexRGB:0x6AA9CF]],
];
self.indexPathController.items = items;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [super collectionView:collectionView cellForItemAtIndexPath:indexPath];
//retrieve the cell data for the given index path from the controller
//and set the cell's text label and background color
NSArray *item = [self.indexPathController.dataModel itemAtIndexPath:indexPath];
UILabel *label = (UILabel *)[cell viewWithTag:1];
label.text = item[IDX_TEXT];
cell.backgroundColor = item[IDX_COLOR];
cell.layer.cornerRadius = 10;
return cell;
}

- (void)shuffle
{
//shuffle the items randomly and update the controller with the shuffled items
NSMutableArray *shuffledItems = [NSMutableArray arrayWithArray:self.indexPathController.items];
NSInteger count = shuffledItems.count;
for (int i = 0; i < count; i++) {
[shuffledItems exchangeObjectAtIndex:i withObjectAtIndex:arc4random() % count];
}
self.indexPathController.items = shuffledItems;
}

@end

此示例源自提供的 TLCollectionViewController基类,但您可以通过从 TLCollectionViewController 复制/粘贴轻松地将 TLIndexPathTools 集成到您自己的 View Controller 中(那里很少发生)。

它还改进了 NSFetchedResultsController在某些方面。首先,您的项目不需要预先分类以组织成部分。其次,您可以进行动画排序和过滤(如果您使用 NSFetchedResultsController 更改排序描述符或谓词,您必须重新执行提取并且不会获得动画)。尝试运行 Core Data sample project .

当前版本旨在扩展到 1000 多个项目。因此,如果您有一个非常大的数据集,您可能会遇到性能问题。

关于iOS:使用没有核心数据的动画更新表格 View 数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18189409/

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