我有一个collectionView
列出了一个基于数组的配方和一个带有参数来创建新配方的“添加配方”segue。虽然当我点击完成按钮(在addRecipeViewController.m
中)并触发addNewRecipe方法(在RecipeViewController.m
中)时,一切正常(所有帐户),我无法让collectionView更新数据我刚刚进入有人能帮助我吗?
我尝试添加以下内容(来自RecipesViewController
):
- (void)addRecipeViewController:(AddRecipeViewController *)controller didAddRecipe:(BeerRecipe *)recipe toCollectionView:(UICollectionView *)collectionView
{
[self.recipes addObject:recipe];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([self.recipes count] - 1) inSection:0];
[collectionView insertItemsAtIndexPaths:@[indexPath]];
[self dismissViewControllerAnimated:YES completion:nil];
}
我收到了
无法识别的选择器发送到实例
错误,我认为我错误地使用了collectionView
。我也尝试了自己。collectionView
但我收到了“collectionView
”类型对象上找不到的“RecipesViewController
个属性。”
提前致谢。
编辑:好的,所以我没有在.h文件中正确声明collectionView所以一旦没问题,我就可以使用:
[self.collectionView insertItemsAtIndexPaths:@ [indexPath]];
从那里开始,collectionView仍然没有更新以反映更改,因此经过一些调试后,我发现collectionView计数无法正常工作。
-(NSInteger) collectionView:(UICollectionView *) collectionView numberOfItemsInSection:(NSInteger)section {
return [self.recipes count];
}
我的初始数组只有2个项目,所以一旦我将返回值改为“3”,我就可以在collectionView单元格中显示这个条目。所以我现在的问题是,确保在创建新数组条目后更新self.recipes计数的最佳方法是什么?
编辑2:我仍然无法纠正这个问题,尽管改变了顺序以及在viewDidLoad中初始化数组,如下所示。
self.recipes = [[NSMutableArray alloc] init];
如果我这样做,我得到这个错误 - 索引0超出空数组的界限 - 在下一行:
BeerRecipe *recipe = (self.recipes)[indexPath.row];
BeerRecipe.h
#import <Foundation/Foundation.h>
@interface BeerRecipe : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *beerstyle;
@property (nonatomic, copy) NSString *beertype;
@property (nonatomic, copy) NSString *parentRecipe;
@property (nonatomic, assign) NSInteger ibu;
@property (nonatomic, assign) double og;
@property (nonatomic, assign) double fg;
@property (nonatomic, assign) NSInteger timesBrewed;
@property (nonatomic, assign) NSDate *dateCreated;
@end
BeerRecipe.m
#import "BeerRecipe.h"
@implementation BeerRecipe
@end
RecipesViewController.h
#import <UIKit/UIKit.h>
#import "AddRecipeViewController.h"
@interface RecipesViewController : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource, AddRecipeViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *beer_image;
@property (nonatomic, strong) NSMutableArray *recipes;
@end
RecipesViewController.m
#import "RecipesViewController.h"
#import "BeerRecipe.h"
#import "AddRecipeViewController.h"
@interface RecipesViewController ()
@end
@implementation RecipesViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma marks Collection Methods->
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger) collectionView:(UICollectionView *) collectionView numberOfItemsInSection:(NSInteger)section {
return [self.recipes count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UILabel *beernamelabel = (UILabel *) [cell viewWithTag:1];
UILabel *beerstylelabel = (UILabel *) [cell viewWithTag:2];
//UILabel *creationdatelabel = (UILabel *) [cell viewWithTag:3];
UILabel *timesbrewedlabel = (UILabel *) [cell viewWithTag:4];
UILabel *parentrecipelabel = (UILabel *) [cell viewWithTag:5];
UILabel *beertypelabel = (UILabel *) [cell viewWithTag:6];
UILabel *oglabel = (UILabel *) [cell viewWithTag:7];
UILabel *fglabel = (UILabel *) [cell viewWithTag:8];
UILabel *ibulabel = (UILabel *) [cell viewWithTag:9];
BeerRecipe *recipe = (self.recipes)[indexPath.row];
beernamelabel.text = recipe.name;
beerstylelabel.text = recipe.beerstyle;
//creationdatelabel.text = recipe.dateCreated;
beertypelabel.text = recipe.beertype;
timesbrewedlabel.text = [NSString stringWithFormat:@"%d", recipe.timesBrewed];
parentrecipelabel.text = recipe.parentRecipe;
oglabel.text = [NSString stringWithFormat:@"%.3lf", recipe.og];
fglabel.text = [NSString stringWithFormat:@"%.3lf", recipe.fg];
ibulabel.text = [NSString stringWithFormat:@"%d", recipe.ibu];
return cell;
}
- (void)addRecipeViewController:(AddRecipeViewController *)controller didAddRecipe:(BeerRecipe *)recipe toCollectionView:(UICollectionView *)collectionView
{
[self.recipes addObject:recipe];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([self.recipes count] - 1) inSection:0];
[collectionView insertItemsAtIndexPaths:@[indexPath]];
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - AddRecipeViewControllerDelegate
- (void)AddRecipeViewControllerDidCancel:(AddRecipeViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)AddRecipeViewControllerDidSave:(AddRecipeViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"AddRecipe"]) {
UINavigationController *navigationController = segue.destinationViewController;
AddRecipeViewController *addRecipeViewController = [navigationController viewControllers][0];
addRecipeViewController.delegate = self;
}
}
AddRecipeViewController.h
#import <UIKit/UIKit.h>
#import "BeerRecipe.h"
@class AddRecipeViewController;
@protocol AddRecipeViewControllerDelegate <NSObject>
- (void)AddRecipeViewControllerDidCancel:(AddRecipeViewController *)controller;
- (void)AddRecipeViewController:(AddRecipeViewController *)controller didAddRecipe:(BeerRecipe *)recipe;
@end
@interface AddRecipeViewController : UITableViewController
@property (weak, nonatomic) IBOutlet UITextField *recipeName;
@property (weak, nonatomic) IBOutlet UISegmentedControl *shareRecipe;
@property (weak, nonatomic) IBOutlet UILabel *styleLabel;
@property (nonatomic, weak) id <AddRecipeViewControllerDelegate> delegate;
- (IBAction)cancel:(id)sender;
- (IBAction)done:(id)sender;
@end
AddRecipeViewController.m
#import "AddRecipeViewController.h"
#import "BeerRecipe.h"
#import "RecipesViewController.h"
@interface AddRecipeViewController ()
@end
@implementation AddRecipeViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (IBAction)cancel:(id)sender
{
[self.delegate AddRecipeViewControllerDidCancel:self];
}
- (IBAction)done:(id)sender
{
BeerRecipe *recipe = [[BeerRecipe alloc]init];
recipe.name = self.recipeName.text;
[self.delegate AddRecipeViewController:self didAddRecipe:recipe];
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
[self.recipeName becomeFirstResponder];
}
}
@end
CGAppDelegate.h
#import <UIKit/UIKit.h>
@interface CGAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
CGAppDelegate.m
#import "CGAppDelegate.h"
#import "BeerRecipe.h"
#import "RecipesViewController.h"
@implementation CGAppDelegate {
NSMutableArray *_recipes;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//_recipes = [[NSMutableArray alloc] init];
_recipes = [NSMutableArray arrayWithCapacity:20];
BeerRecipe *recipe = [[BeerRecipe alloc] init];
recipe.name = @"Sierra Nevada Clone v1";
recipe.beerstyle = @"American Pale Ale";
recipe.beertype = @"All Grain";
recipe.dateCreated = [NSDate date];
recipe.ibu = 60;
recipe.og = 1.012;
recipe.fg = 1.006;
recipe.timesBrewed = 0;
recipe.parentRecipe = @"Nil";
[_recipes addObject:recipe];
recipe = [[BeerRecipe alloc] init];
recipe.name = @"Celebration Ale";
recipe.beerstyle = @"American IPA";
recipe.beertype = @"All Grain";
recipe.dateCreated = [NSDate date];
recipe.ibu = 60;
recipe.og = 1.012;
recipe.fg = 1.006;
recipe.timesBrewed = 0;
recipe.parentRecipe = @"Nil";
[_recipes addObject:recipe];
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UINavigationController *navigationController = [tabBarController viewControllers][1];
RecipesViewController *recipesViewController = [navigationController viewControllers][0];
recipesViewController.recipes = _recipes;
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
答案 0 :(得分:0)
添加完食谱[self.recipes addObject:recipe]
之后,您需要重新加载您的收藏集{1}}