只有数组中的最后一个对象填充一个NSMutableArray列表

问题描述:

我已经创建了一个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

您正在添加相同的对象。 在每个addObject之后使用varietal =[[wine alloc] init];来创建不同的值。

其实,你有一个对象(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将导致内存泄漏。