【问题标题】:What causes the table cells to be displayed at random/unknown pattern?是什么导致表格单元格以随机/未知模式显示?
【发布时间】:2011-06-11 23:49:50
【问题描述】:

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

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

这是一个每次都具有相同结果的示例。以下是我刚刚添加 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

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

这是什么原因?谢谢!

来自 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;
}

【问题讨论】:

    标签: objective-c ios ios4 uitableview xcode4


    【解决方案1】:

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

    【讨论】:

    • 你链接到的线程给了我一些很棒的提示!看来我只需要一个枚举器来帮助重新组织数据。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多