我在使用NSMutableArray
和NSMutableDictionary
时遇到问题。
我创建了一个测验,当它停止时,它应该在tableview
中显示当前得分和日期。然后它应该在tableview
中显示这些值。但是当我加载viewController
时,tableview中没有显示任何内容。 mutableArray
是在viewDidLoad:
方法中创建的。我的代码是否正确,或者我在这里遗漏了什么?
- (IBAction)saveResult:(id)sender {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:NSLocalizedString(@"DATE", nil)];// @"HH:mm zz"];
dateNow = [NSDate date];
NSString *formattedDateString = [dateFormatter stringFromDate:dateNow];
// Add the quiz result to the array, reload the data in the tableview
highscoreAndDate = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:@"%i points", score], @"kResult",
formattedDateString, @"kDate", nil];
[highscore addObject:highscoreAndDate];
[tableView reloadData];
}
我的tableView方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [highscore count];
}
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat: @"HH:mm zz"];
cell.detailTextLabel.text = [[highscore objectAtIndex:indexPath.row] objectForKey:@"kResult"];
cell.detailTextLabel.text = [[highscore objectAtIndex:indexPath.row] objectForKey:@"kDate"];
return cell;
}
答案 0 :(得分:0)
@Anoop Vaidya。
你让我走在正确的轨道上。我在添加对象时放了NSLog语句,它显示我的数组的行数正在增加。所以我的代码是正确的。
我发现这是由于我在XIB文件中的连接(非常尴尬!)。本应该发现自己。我把我的tableview出口连接到了错误的viewController,因此从未填充过tableview。
非常抱歉占用了你的时间并感谢所有回复如此之快的人。很高兴我把它修好了。 :)