2

我在使用NSMutableArrayand时遇到问题NSMutableDictionary

我创建了一个测验,当它停止时,它应该在tableview. 然后它应该将这些值显示在tableview. 但是当我加载它的viewController. 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;
}
4

1 回答 1

0

@Anoop Vaidya。

你让我走上了正确的轨道。我在添加对象时放置了 NSLog 语句,它显示我的数组的行数正在增加。所以我的代码是正确的。

我发现这是由于我在我的 XIB 文件中的连接(非常尴尬!)。应该发现我的自我。我已将我的 tableview 插座连接到错误的 viewController,因此 tableview 从未填充过。

非常抱歉占用了您的时间,并感谢所有回复如此迅速的人。很高兴我把它修好了。:)

于 2013-04-03T07:39:48.063 回答