objective-c - 是什么导致表格单元格以随机/未知模式显示?

标签 objective-c ios ios4 uitableview xcode4

我正在创建的应用程序以表格 View 启动。我可以立即添加新信息,将信息保存到 plist,然后重新加载 View 。新单元格及其数据显示在底部。然而,当我重新运行该应用程序时,它会重新排列我最初看到它们的顺序。

单元格的标题存储在名为 countdowns 的变量中,描述存储在 countdown_info 中。然后我使用这些变量并将它们作为字典存储在 plist 中。

这是一个每次都有相同结果的例子。下面是表格 View ,如果我刚刚添加了 4 个新单元格,它将显示:

**[CODE 1]**
Countdown 1
  Description 1
Countdown 2
  Description 2
Countdown 3
  Description 3
Countdown 4
  Description 4

但是,当应用程序重新运行/重新构建时,应用程序将打开:

**[View After Re-Run]**
Countdown 4
  Description 4
Countdown 1
  Description 1
Countdown 3
  Description 3
Countdown 2
  Description 2

我添加的一些 NSLog 显示文件中的表被保存为 [代码 1],并且 TableView 上的 indexPath.row 从 0 增加到 3,但由于某种原因, View 以不同的模式重新组织.

这是什么原因?谢谢!

来自 cellRowAtIndexPath 的代码

- (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:@"Cell"] autorelease];
}

ClockworkAppDelegate *dataCenter = (ClockworkAppDelegate *)[[UIApplication sharedApplication] delegate];

cell.textLabel.text = [[dataCenter countdowns] objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [[dataCenter countdown_info] objectAtIndex:indexPath.row];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

return cell;
}

最佳答案

NSDictionaries 是 unordered ,因此在保存到 NSUserDefaults 时您可能会丢失顺序。您还必须以某种方式存储订单。参见 this thread一些想法。

关于objective-c - 是什么导致表格单元格以随机/未知模式显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6319228/

相关文章:

objective-c - 在 dealloc 中将 IBOutlets 设置为 nil

ios - Swift 3 - 如何让我的计时器在后台计数?

ios - 如何使用 UIView 框架创建自定义初始化

ios - UITapGestureRecognizer 并在点击背景时使键盘消失

xcode - 取消 [self PerformSelector :. .. withObject:nil afterDelay:20];

ios - GMSPlacePickerViewController 中的 UI 偏移量

objective-c - objective-c 中的(覆盖)是什么

ios - 在文本字段中复制/粘贴光标语言

iphone - 为什么 fabs 返回整数?

iphone - 在 xcode 4.2 上播放时没有背景音乐