2

と の使用に問題がNSMutableArrayあり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 に答える