简体   繁体   中英

Disappearing NSMutableArray, causes UiTableView to show blank cells

I've got a UITableView which shows a list of recipes. I create each recipe with a custom class, Recipe, and then put the recipes into a msmutable array, 'recipes'. The trouble is, when I start scrolling the 'recipes' array seems to lose all of its data (the count is 0). This means that the cells which come into view cannot display any recipe info. What exactly is happening? Why is the array disappearing?

Code is below:

// .h

#import "CustomCell.h"
#import "Recipe.h"
@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *recipeTableView;
@property (weak, nonatomic) NSMutableArray *recipes;


// .m
@synthesize recipeTableView;
@synthesize Recipes;

- (void)viewDidLoad
{
    [super viewDidLoad];
   Recipe *recipe1 = [[Recipe alloc] initWithCategory:@"Seafood" title:@"Recipe 1"];
    recipe1.recipeImage = [UIImage imageNamed:@"recipeImage1.png"];

    Recipe *recipe2 = [[Recipe alloc] initWithCategory:@"Seafood" title:@"Recipe 2"];
    recipe2.recipeImage = [UIImage imageNamed:@"recipeImage2.png"];

    Recipe *recipe3 = [[Recipe alloc] initWithCategory:@"Seafood" title:@"Recipe 3"];
    recipe3.recipeImage = [UIImage imageNamed:@"recipeImage3.png"];

    Recipe *recipe4 = [[Recipe alloc] initWithCategory:@"Seafood" title:@"Recipe 4"];
    recipe4.recipeImage = [UIImage imageNamed:@"recipeImage4.png"];

    Recipe *recipe5 = [[Recipe alloc] initWithCategory:@"Seafood" title:@"Recipe 5"];
    recipe5.recipeImage = [UIImage imageNamed:@"recipeImage5.png"];

    Recipe *recipe6 = [[Recipe alloc] initWithCategory:@"Seafood" title:@"Recipe 6"];
    recipe6.recipeImage = [UIImage imageNamed:@"recipeImage6.png"];

    Recipe *recipe7 = [[Recipe alloc] initWithCategory:@"Seafood" title:@"Recipe 7"];
    recipe7.recipeImage = [UIImage imageNamed:@"recipeImage7.png"];

    Recipe *recipe8 = [[Recipe alloc] initWithCategory:@"Seafood" title:@"Recipe 8"];
    recipe8.recipeImage = [UIImage imageNamed:@"recipeImage8.png"];

    Recipe *recipe9 = [[Recipe alloc] initWithCategory:@"Seafood" title:@"Recipe 9"];
    recipe9.recipeImage = [UIImage imageNamed:@"recipeImage9.png"];

    Recipe *recipe10 = [[Recipe alloc] initWithCategory:@"Seafood" title:@"Recipe 10"];
    recipe10.recipeImage = [UIImage imageNamed:@"recipeImage10.png"];


    recipes = [NSMutableArray arrayWithObjects:recipe1, recipe2, recipe3, recipe4, recipe5, recipe6, recipe7, recipe8, recipe9, recipe10, nil];

}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Recipe *recipe = (Recipe*)[recipes objectAtIndex:indexPath.row];
    static NSString *CellIdentifier = @"RecipeCell";
    CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLabel.text = recipe.title;
    return cell;
}

Your array is being autoreleased, add the following after the recipes assignment...

[recipes retain];

OR change the way you create the array to...

recipes = [[NSMutableArray alloc] initWithObjects:.... etc

If you are using ARC in iOS5 then you should declare your recipes variable as strong which effectively does the retain for you.

@simon is right. in addition recipeTableView should be (nonatomic, strong) as you want it to be retained as long as your view is loaded. in general, I'd err on the side of using strong vs weak unless/until you have a specific reason for needing one. As you get more comfortable with memory management and object lifecycles you'll discover situations where weak is desirable/required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM