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;
}
동일한 개체를 반복해서 계속 추가합니다. *** 다른 *** 개체를 사용하십시오. –
(힌트 :'[[wine alloc] init]'은 객체를 생성합니다.) –
(객체 지향 프로그래밍에 관한 좋은 텍스트를 찾아서 공부하십시오.) –