Objective c 消失NSMutableArray,导致UiTableView显示空白单元格

Objective c 消失NSMutableArray,导致UiTableView显示空白单元格,objective-c,ios,uitableview,nsmutablearray,automatic-ref-counting,Objective C,Ios,Uitableview,Nsmutablearray,Automatic Ref Counting,我有一个UITableView,它显示了一个食谱列表。我用一个自定义类recipe创建每个菜谱,然后将菜谱放入一个msmutable数组“recipes”。问题是,当我开始滚动“recipes”数组时,它似乎丢失了所有数据(计数为0)。这意味着进入视图的单元格不能显示任何配方信息。到底发生了什么?为什么阵列会消失 代码如下: // .h #import "CustomCell.h" #import "Recipe.h" @interface ViewController : UIViewCon

我有一个UITableView,它显示了一个食谱列表。我用一个自定义类recipe创建每个菜谱,然后将菜谱放入一个msmutable数组“recipes”。问题是,当我开始滚动“recipes”数组时,它似乎丢失了所有数据(计数为0)。这意味着进入视图的单元格不能显示任何配方信息。到底发生了什么?为什么阵列会消失

代码如下:

// .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;
}
/.h
#导入“CustomCell.h”
#导入“Recipe.h”
@界面ViewController:UIViewController
@属性(弱,非原子)IBUITableView*recipeTableView;
@属性(弱、非原子)NSMutableArray*配方;
//m
@综合交互视图;
@综合配方;
-(无效)viewDidLoad
{
[超级视图下载];
配方*recipe1=[[Recipe alloc]initWithCategory:@“海鲜”标题:@“配方1”];
recipe1.recipeImage=[UIImage ImageName:@“recipeImage1.png”];
配方*recipe2=[[Recipe alloc]initWithCategory:@“海鲜”标题:@“配方2”];
recipe2.recipeImage=[UIImage ImageName:@“recipeImage2.png”];
配方*recipe3=[[Recipe alloc]initWithCategory:@“海鲜”标题:@“配方3”];
recipe3.recipeImage=[UIImage ImageName:@“recipeImage3.png”];
配方*recipe4=[[Recipe alloc]initWithCategory:@“海鲜”标题:@“配方4”];
recipe4.recipeImage=[UIImage ImageName:@“recipeImage4.png”];
配方*recipe5=[[Recipe alloc]initWithCategory:@“海鲜”标题:@“配方5”];
recipe5.recipeImage=[UIImage ImageName:@“recipeImage5.png”];
配方*recipe6=[[Recipe alloc]initWithCategory:@“海鲜”标题:@“配方6”];
recipe6.recipeImage=[UIImage ImageName:@“recipeImage6.png”];
配方*recipe7=[[Recipe alloc]initWithCategory:@“海鲜”标题:@“配方7”];
recipe7.recipeImage=[UIImage ImageName:@“recipeImage7.png”];
配方*recipe8=[[Recipe alloc]initWithCategory:@“海鲜”标题:@“配方8”];
recipe8.recipeImage=[UIImage ImageName:@“recipeImage8.png”];
配方*recipe9=[[Recipe alloc]initWithCategory:@“海鲜”标题:@“配方9”];
recipe9.recipeImage=[UIImage ImageName:@“recipeImage9.png”];
配方*recipe10=[[Recipe alloc]initWithCategory:@“海鲜”标题:@“配方10”];
recipe10.recipeImage=[UIImage ImageName:@“recipeImage10.png”];
recipes=[NSMutableArray数组,其对象为: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];
静态NSString*CellIdentifier=@“RecipeCell”;
CustomCell*单元格=(CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.titleLabel.text=recipe.title;
返回单元;
}

您的数组正在自动删除,请在配方分配后添加以下内容

[recipes retain];
或者将创建数组的方式更改为

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

如果您在iOS5中使用ARC,那么您应该将您的recipes变量声明为strong,这将有效地为您保留参数。

@simon是正确的。此外,
recipeTableView
应该是(非原子的,强的),只要加载视图,它就会被保留。一般来说,我倾向于使用强项vs弱项,除非/直到你有一个需要强项的具体原因。随着您对内存管理和对象生命周期越来越熟悉,您将发现需要/需要弱的情况。

像这样声明属性:
@property(strong,nonatomic)NSMutableArray*显示谢谢,改为“强工作”。我第一次进入ARC是令人困惑的。