我创建了一个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;
}
答案 0 :(得分:1)
实际上,你有一个你变异的对象(varietal
)(意味着你改变了它的属性)。
所以你真的只需要在数组中添加一个对象。
要解决此问题,将发布 varietal
添加到数组(数组保留它)并创建新对象后添加:< / p>
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
将导致内存泄漏。
答案 1 :(得分:1)
您正在添加相同的对象。
每varietal =[[wine alloc] init];
后使用addObject
创建不同的。{/ p>