0

나는 요리법 목록을 보여주는 UITableView 있어요. 커스텀 클래스 인 Recipe로 각각의 래서 피를 생성하고, 래서 피를 'recipes'라는 msmutable 배열에 저장합니다. 문제는 내가 '래서 피'배열을 스크롤하기 시작하면 모든 데이터를 잃어버린 것처럼 보입니다 (카운트는 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

다음과 같은 속성을 선언하십시오. NSMutableArray * shows; ' – beryllium

답변

1

귀하의 배열이 조리법을 할당 한 후 다음을 추가, 오토 릴리즈되고

[recipes retain]; 

또는 당신이 배열을 생성하는 방법을 변경 ... :

코드는 다음과 같습니다

iOS5에서 ARC를 사용하는 경우 요리법 변수를 strong으로 선언해야합니다. 효과적으로 당신을 위해 보유합니다.

+0

강해서로 바뀌어 감사드립니다. ARC에 대한 나의 첫 번째 침탈은 혼란 스러웠습니다. – cannyboy

0

@ 사이먼이 맞습니다. 또한 recipeTableView은보기가로드되는 한 계속 유지해야하므로 (비 원자력, 강함)이어야합니다. 일반적으로, 당신이 필요로하는 특별한 이유가있을 때까지/강한 약점을 사용하는 측면에서 나는 잘못을 범할 것이다. 메모리 관리 및 객체 라이프 사이클에 익숙해지면 약한 것이 바람직한/어려운 상황을 발견하게됩니다.