1

我创建了一个 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;
}
4

2 回答 2

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会导致内存泄漏。

于 2013-04-21T20:08:50.413 回答
1

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

于 2013-04-21T20:08:59.670 回答