0

对,所以我的 UITableView 加载并按字母顺序放置所有单元格。然后从服务器下载信息并完成计算,一旦一切完成,就会重新加载 TableView。目前这是一个非常简单的过程,因为一旦从服务器下载了信息,单元格甚至不会移动,它们按字母顺序排列。除了填写了一半的信息并根据计算进行了微小的更改之外,什么都没有发生。我想知道是否有一种简单的方法可以根据下载完成后完成的计算将单元格分成几部分?我确实有一个创建 4 个数组的想法(永远只有 4 个部分),一旦 isLoading 设置为 no,将 TableView 的数据源更改为有部分,但是,这听起来有点...... 不确定。我知道这是一个理论问题,而不是编码问题,但是在我去弄乱我的代码之前,肯定是一种愚蠢低效的做事方式,是否有一种简单的方法可以将 UITableViewCells “分配”到部分?

我这样做的主要问题是,如果用户删除一个单元格,删除 Core Data 中的相应条目会有点棘手并且容易出错。这让我想到了另一个想法。如果我向我的核心数据实体添加一个额外的属性会怎样。该属性将被分配,然后在计算完成后保存。这样做的问题是没有现有的数据库可以工作。必须有一种巧妙的方法来实现这一点。

谢谢您的帮助。如果您需要我发布任何代码,请直接说出来,我会的。

4

1 回答 1

1

You should be fine if you implement the data source methods related to sections.

For example:

  • numberOfSectionsInTableView
  • sectionIndexTitlesForTableView.

Any time the table data is reloaded (e.g., [self.tableView reloadData]), these methods will be called and the data will be placed into their sections.

Keep in mind that the cells are just the visual representation of your model, which in this case is your fetched data. The cells are not assigned to sections; they are simply created however you specify for your model (via the table view data source and delegate methods).

Regarding deletion of entries while using Core Data, I suggest taking a look at NSFetchedResultsController. The latter will monitor any changes to your table's data and message its delegate, your table view controller, when updates are made.

For example, a deletion would start with a call to the table view delegate like normal (i.e., via tableView:didEndEditingRowAtIndexPath). Within the latter, you would then delete the entry from core data (e.g., [self.myDatabase.managedObjectContext deleteObject:entity]). Assuming you initiated the NSFetchedResultsController w/ the same managed object context, the deletion would be automatically reflected back to your user.

If you're using a remote DB, however, you'll also have to perform a save (however you've implemented that) to ensure the DB is updated too.

Note also that if you use an NSFetchedResultsController, you don't need to implement the section data source methods since NSFetchedResultsController can handle that for you. Just define the key-path in your data model that will return the section name when initializing the NSFetchedResultsController.

于 2013-01-03T15:59:24.170 回答