【问题标题】:NSMutableArray and NSMutableDictionary in a TableViewTableView 中的 NSMutableArray 和 NSMutableDictionary
【发布时间】:2013-03-24 16:47:22
【问题描述】:

我在使用 NSMutableArrayNSMutableDictionary 时遇到问题。

我创建了一个测验,当它停止时,它应该在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;
}

【问题讨论】:

  • 显示查看 didLoad 方法。
  • 这不是关于 Xcode 的问题。
  • 在 cellForRowAtIndexPath 放置断点并检查它是否运行?如果它没有运行,那么把你的逻辑放在 viewWillAppear 方法上:)
  • 你有alloc+init高分数组吗?通过 NSLog(highscore.count) 签入 numberofrowsinSection;
  • 显示您的 viewdidload 代码可能是您在创建数组之前加载 tableview

标签: ios objective-c nsmutablearray nsmutabledictionary


【解决方案1】:

@Anoop Vaidya。

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    • 2014-04-13
    相关资源
    最近更新 更多