我很.愧于此.这是我的场景在我的Appdelegate,我正在创造
>视图控制器的一个实例,将以模态方式呈现,从用户收集两个数据
>一个tableView控制器
>一个导航控制器,我用tableview控制器作为其rootview进行初始化
调节器
>一个collectionviewController,请注意,我也在这里注册单元格的类.
> UITabBarController,我将导航控制器添加为第一个视图和
collectionview作为第二个视图.. TabBarCOntorller被设置为窗口的根视图.
这是代码:
- (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有一个导航栏按钮,显示模型视图控制器.模态视图控制器具有一个按钮,它从textField和datepicker获取值,并使用UserInfo字典中的值将其发布到默认通知中心. tableView控制器订阅通知,将UserInfo字典放在MutableArray中并更新表.工作正常
问题是CollectionVIew. collectionVIew接收通知,并调用目标方法来处理通知.我正在尝试从通知中获取UserInfo字典,并将一个新单元格添加到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中还是破了.)