0

我有一个UITableView它显示配方列表。我使用自定义类Recipe创建每个食谱,然后将食谱放入一个msmutable数组'recipe'中。麻烦的是,当我开始滚动'食谱'数组似乎失去了所有的数据(计数为0)。这意味着进入视图的单元格不能显示任何配方信息。究竟发生了什么?为什么阵列消失?消失的NSMutableArray,导致的UITableView显示空白单元格

代码如下:

// .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; 
} 
+0

财产申报是这样的:'@property(强,非原子)的NSMutableArray *显示;' – beryllium

回答

1

你的阵列被自动释放,添加配方分配后以下...

[recipes retain]; 

或者改变您创建数组的方式...

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

如果你在iOS5中使用ARC,那么你应该声明你的食谱变量为强有效地为你保留。

+0

谢谢,转变为强大的工作。我对ARC的第一次尝试令人困惑。 – cannyboy

0

@simon是正确的。另外recipeTableView应该是(非原子的,强壮的),只要你的视图被加载就可以保留它。总的来说,除非你有一个特定的理由需要一个,否则我会在使用强对弱方面犯错。随着内存管理和对象生命周期的变得更加舒适,您将发现需要/不需要弱点的情况。