【发布时间】:2013-03-24 16:47:22
【问题描述】:
我在使用 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;
}
【问题讨论】:
-
显示查看 didLoad 方法。
-
这不是关于 Xcode 的问题。
-
在 cellForRowAtIndexPath 放置断点并检查它是否运行?如果它没有运行,那么把你的逻辑放在 viewWillAppear 方法上:)
-
你有alloc+init高分数组吗?通过 NSLog(highscore.count) 签入 numberofrowsinSection;
-
显示您的 viewdidload 代码可能是您在创建数组之前加载 tableview
标签: ios objective-c nsmutablearray nsmutabledictionary