我有一個collectionView
,它列出了基於數組的食譜和一個「添加配方」的segue,它使用參數來創建一個新配方。雖然當我點擊完成按鈕(在addRecipeViewController.m
)並觸發addNewRecipe方法(在RecipeViewController.m
),但我無法讓collectionView更新剛纔輸入的數據,所有這些都可以正常工作。有人能幫我嗎?更新數組並插入新的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
從代碼中刪除不需要的方法。 – Tirth
從您向我們展示的內容中,您沒有在鏈接到您在RecipesViewController中使用的UICollectionView的.h文件中聲明collectionView屬性。所以這種方法,錯誤,是不可能的,直到你有。 –
您需要在調用insertItemsAtIndexPaths之前更新數據源(即'self.recipes')。這就是你的「count」不正確的原因。 – Gad