【问题标题】:Disappearing NSMutableArray, causes UiTableView to show blank cells消失的 NSMutableArray,导致 UiTableView 显示空白单元格
【发布时间】:2011-11-03 16:09:30
【问题描述】:

我有一个显示食谱列表的 UITableView。我使用自定义类Recipe 创建每个食谱,然后将这些食谱放入一个msmutable 数组“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;
}

【问题讨论】:

  • 像这样声明属性:@property (strong, nonatomic) NSMutableArray *shows;

标签: objective-c ios uitableview nsmutablearray automatic-ref-counting


【解决方案1】:

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

[recipes retain];

或者将创建数组的方式更改为...

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

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

【讨论】:

  • 谢谢,改用强工作。我对 ARC 的第一次尝试令人困惑。
【解决方案2】:

@simon 是对的。此外,recipeTableView 应该是 (nonatomic, strong),因为您希望在加载视图时保留它。一般来说,除非/直到你有特定的理由需要一个,否则我会在使用强与弱方面犯错。随着您对内存管理和对象生命周期越来越熟悉,您会发现需要/需要弱的情况。

【讨论】:

    猜你喜欢
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多