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