ios - 无效更新 : invalid number of items on UICollectionView

标签 ios objective-c uitableview uicollectionview

我对此感到难过。这是我的场景。在我的 Appdelegate 中,我正在创建

  1. 将以模态方式呈现以从用户收集两条数据的 View Controller 实例
  2. 一个 tableView Controller
  3. 我使用 tableview Controller 作为其 Root View 初始化的导航 Controller
    控制者
  4. 一个 collectionviewController,请注意,我也在此处为单元格注册类。
  5. 我将导航 Controller 添加到的 UITabBarController 作为第一个 View 和
    collectionview 作为第二个 View 。TabBarCOntorller 被设置为窗口的 Root View 。

代码如下:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:   
(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.evc = [[WTAEntryControllerViewController alloc] init];
WTATableView *tv = [[WTATableView alloc] initWithNibName:@"WTATableView" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc]    
initWithRootViewController:tv];
nav.tabBarItem.title = NSLocalizedString(@"Birthday List", nil);
nav.tabBarItem.image = [UIImage imageNamed:@"birthdaycake"];
WTACollectionViewController *cv = [[WTACollectionViewController alloc] 
initWithCollectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
[cv.collectionView registerClass:[UICollectionViewCell class] 
forCellWithReuseIdentifier:CellIdentifier];
cv.collectionView.backgroundColor = [UIColor whiteColor];
NSLog(@"cv instance %@", cv);
UITabBarController *tabController = [[UITabBarController alloc] init];
tabController.viewControllers = @[nav, cv];
self.window.rootViewController = tabController;
[self.window makeKeyAndVisible];
return YES
}

tableview 有一个导航栏按钮,用于显示 Model View Controller 。模态视图 Controller 有一个按钮,它从文本字段和日期选择器中获取值,并将其与 UserInfo 字典中的值一起发布到默认通知中心。 tableView contorller 订阅通知,将 UserInfo 字典放入 MutableArray 并更新表。那很好用。

问题出在 CollectionView 上。 collectionVIew 接收通知并调用目标方法来处理通知。我正在尝试从通知中获取 UserInfo 字典并向 collectionView 添加一个新单元格。

但是...

当 collectionview 收到通知时,我收到错误:

'无效更新:第 0 部分中的项目数无效。更新 (1) 后现有部分中包含的项目数必须等于更新 (1) 前该部分中包含的项目数,加上或减去从该部分插入或删除的项目数(插入 1 个,删除 0 个),加上或减去移入或移出该部分的项目数(移入 0 个,移出 0 个)。'

这是 CollectionView 的代码:

#import "WTACollectionViewController.h"
#import "UIColor+WTAExtensions.h"
#import "WTAAppDelegate.h"
#import "WTAEntryControllerViewController.h"

@interface WTACollectionViewController () <UICollectionViewDelegateFlowLayout>
@property (strong, nonatomic) NSMutableArray *birthdays;
@end

@implementation WTACollectionViewController

-(id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout{

NSLog(@"Calling init on Collection");
if(!(self = [super initWithCollectionViewLayout:layout])){
    return nil;
}

self.birthdays = [NSMutableArray array];

NSLog(@"Count of Birthdays at init %ld", (long)[self.birthdays count] );
self.title = NSLocalizedString(@"Birthday Grid", nil);
self.tabBarItem.image = [UIImage imageNamed:@"package"];
NSLog(@"cv instance getting notif %@", self);
[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(handleNotification:)
 name:@"BirthdayAdded"
 object:nil];
 return self;

}


-(void)handleNotification:(NSNotification *)notification
{
[self.birthdays addObject:[notification userInfo]];
NSLog(@"Count of birthdays when the notification is received: %ld", (long)         
[self.birthdays count]);
[self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:   
(self.birthdays.count -1) inSection:0]]];
}

- (void)viewDidLoad
{
[super viewDidLoad];

NSLog(@"Calling View Did Load on Collection");
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

return 1;

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:   
(NSInteger)section{

NSLog(@"Count of birthdays in  the number of items in section: %ld", (long)  
[self.birthdays count]);
return [self.birthdays count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView    
cellForItemAtIndexPath:(NSIndexPath *)indexPath {

NSLog(@"Calling the cell for index path method");

NSDictionary *dict = [[NSMutableDictionary alloc] init];
dict = self.birthdays[indexPath.item];

UICollectionViewCell *cell = [collectionView   
dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
NSAssert(cell != nil, @"Expected a Cell");

cell.contentView.backgroundColor = [UIColor randomColor];
return cell;

}

#define GAP (1.0f)

-(CGSize)collectionView:(UICollectionView *)collectionView layout:   
(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath   
*)indexPath {


CGFloat edgeLength = self.collectionView.frame.size.width / 4.0f - GAP;
return (CGSize) {.width = edgeLength, .height = edgeLength};
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:   
(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:    
(NSInteger)section{

return GAP;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:   
(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:  
(NSInteger)section{

return GAP;
}
@end

如果有人能在这里指出任何问题,我将不胜感激......

最佳答案

这是在空 UICollectionView 上使用 insertItemsAtIndexPaths 的错误。只需这样做:

if (self.birthdays.count == 1) {
    [self.collectionView reloadData];
} else {
    [self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:   (self.birthdays.count -1) inSection:0]]];
}

(不敢相信这在 iOS8 中仍然是错误的。)

关于ios - 无效更新 : invalid number of items on UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19199985/

相关文章:

iphone - 旋转 iPhone/iPad 时正确分配间距?

objective-c - UITableViewController的表调整大小

objective-c - 错误消息 : "index 1 beyond bounds"

objective-c - 如何在没有菜单快捷方式的情况下关闭 Cmd+W 上的 NSWindow?

iphone - UIPickerView 中的多行选择

ios - 如何处理核心数据保存错误?

ios - 降低 CCSprite 速度时的奇怪行为

ios - 为什么 UIStackView 不断折叠标签?

ios - tableView 中的无限 backgroundView

iOS UITableView 行在 reloadSections 上消失 :withRowAnimation