2013-04-21 57 views
1

我已經創建了一個NSMutableArray;當我運行模擬器時,它只填充單元格中最後一個項目的對象名稱。爲什麼是這樣?只有數組中的最後一個對象填充一個NSMutableArray列表

我相信這個問題至關重要,爲什麼它可能無法正確填充。

@interface WineListTableViewController() 
@end  
@implementation WineListTableViewController  
@synthesize currentVarietal;  

NSMutableArray *list; 

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 

    DescriptionWineViewController *dvc= [segue destinationViewController]; 
    [dvc setCurrentVarietal: [self currentVarietal]]; 

} 


- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 

    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 



    list = [[NSMutableArray alloc] init]; 

    wine *varietal =[[wine alloc] init]; 

    [varietal setName:@"Barbera"]; 
    [varietal setNotes:@"Barbera's Flavor Profile:\n..."]; 
    [list addObject:varietal]; 


    [varietal setName:@"Chenin Blanc"]; 
    [varietal setNotes:@"Chenin Blanc's Flavor Profile:\n... "]; 
    [list addObject:varietal]; 

    [varietal setName:@"Lambrusco"]; 
    [varietal setNotes:@"Lambrusco's Flavor Profile:\n... "]; 
    [list addObject:varietal]; 

    [varietal setName:@"Malbec"]; 
    [varietal setNotes:@"Malbec's Flavor Profile:\n ..."]; 
    [list addObject:varietal]; 

    [varietal setName:@"Merlot"]; 
    [varietal setNotes:@"Merlot's Flavor Profile:\n... "]; 
    [list addObject:varietal]; 

    [varietal setName:@"Montepulciano"]; 
    [varietal setNotes:@"Montepulciano's Flavor Profile:\n... "]; 
    [list addObject:varietal]; 

    [varietal setName:@"Muscat"]; 
    [varietal setNotes:@"Muscat's Flavor Profile:\n... "]; 
    [list addObject:varietal]; 

    [varietal setName:@"Nebbiolo"]; 
    [varietal setNotes:@"Nebbiolo's Flavor Profile:\n... "]; 
    [list addObject:varietal]; 

    [varietal setName:@"Petite Syrah"]; 
    [varietal setNotes:@"Petite Syrah's Flavor Profile:\n... "]; 
    [list addObject:varietal]; 

    [varietal setName:@"Pinot Gris"]; 
    [varietal setNotes:@"Pinot Gris's Flavor Profile:\n... "]; 
    [list addObject:varietal]; 

    [varietal setName:@"Pinot Grigio"]; 
    [varietal setNotes:@"Pinot Grigio's Flavor Profile:\n... "]; 
    [list addObject:varietal]; 

    [varietal setName:@"Pinot Noir"]; 
    [varietal setNotes:@"Pinot Noir's Flavor Profile:\n... "]; 
    [list addObject:varietal]; 

    [varietal setName:@"Sangiovese"]; 
    [varietal setNotes:@"Sangiovese's Flavor Profile:\n... "]; 
    [list addObject:varietal]; 

    [varietal setName:@"Shiraz/ Syrah"]; 
    [varietal setNotes:@"Shiraz/ Syrah's Flavor Profile:\n... "]; 
    [list addObject:varietal]; 

    [varietal setName:@"Viognier"]; 
    [varietal setNotes:@"Viognier's Flavor Profile:\n ..."]; 
    [list addObject:varietal]; 

    [varietal setName:@"Zinfandel"]; 
    [varietal setNotes:@"Zinfandel's Flavor Profile:\n ..."]; 
    [list addObject:varietal]; 



- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    return [list count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell ==nil){ 
     cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

} 

    //this is the code I think must be incorrect 

    wine *current= [list objectAtIndex:indexPath.row]; 
    [cell.textLabel setText:[current name]]; 


    return cell; 
} 
+2

您不斷添加同一個對象。使用***不同的***對象。 – 2013-04-21 20:06:56

+0

(提示:'[[wine alloc] init]'創建一個對象。) – 2013-04-21 20:07:40

+0

(在面向對象的編程中找到一個好文本並研究它。) – 2013-04-21 20:08:11

回答

1

您正在添加相同的對象。 在每個addObject之後使用varietal =[[wine alloc] init];來創建不同的值。

1

其實,你有一個對象(varietal),您變異(這意味着你更改其屬性)。
所以你真的只是添加一個對象到數組中。

爲了解決這個問題,發佈varietal你把它添加後數組(數組保存吧)和創建新對象添加

wine *varietal =[[wine alloc] init]; 
[varietal setName:@"Barbera"]; 
[varietal setNotes:@"Barbera's Flavor Profile:\n..."]; 
[list addObject:varietal]; 
[varietal release]; // Remove this line under ARC (Auto Reference Counting) 

wine *varietal =[[wine alloc] init]; 
[varietal setName:@"Chenin Blanc"]; 
[varietal setNotes:@"Chenin Blanc's Flavor Profile:\n... "]; 
[list addObject:varietal]; 
[varietal release]; // Remove this line under ARC (Auto Reference Counting) 

wine *varietal =[[wine alloc] init]; 
// etc… 

注:如果您正在使用ARC(在當前Xcode版本下的所有新項目的默認設置),您無需致電[varietal release];。它甚至會產生一個錯誤。如果您不使用ARC,則不會釋放varietal將導致內存泄漏。

相關問題