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

发布于 2024-12-13 09:55:35 字数 2724 浏览 0 评论 0原文

我有一个 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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宫墨修音 2024-12-20 09:55:35

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

[recipes retain];

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

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

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

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.

π浅易 2024-12-20 09:55:35

@西蒙是对的。此外,recipeTableView 应该是(非原子的、强的),因为您希望只要加载视图就保留它。一般来说,我会错误地使用强与弱,除非/直到你有一个特定的原因需要一个。当您对内存管理和对象生命周期更加熟悉时,您会发现需要/需要弱的情况。

@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文