简体   繁体   English

核心数据-重新排列TableView不会保存

[英]Core Data - Reordering TableView won't save

.

Hello, 你好,

I have the following code to set the re-ordering of the tableView. 我有以下代码来设置tableView的重新排序。

#pragma mark Row reordering
// Determine whether a given row is eligible for reordering or not.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
// Process the row move. This means updating the data model to <span id="IL_AD6" class="IL_AD">correct</span> the item indices.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
  toIndexPath:(NSIndexPath *)toIndexPath {
NSString *item = [arr objectAtIndex:fromIndexPath.row];
[arr removeObject:item];
[arr insertObject:item atIndex:toIndexPath.row];


}

But when I reload the TableView (note not a TableViewController) The order is set back to what it was before I changed anything. 但是,当我重新加载TableView时(请注意不是TableViewController),该顺序被重新设置为更改任何内容之前的顺序。

Now when I setup the editing for this view (the delete buttons) I had to add this line: 现在,当我为此视图设置编辑(删除按钮)时,我必须添加以下行:

[self.mainTableView setEditing:YES animated:YES];

before it appeared in my view. 才出现在我看来。

Is there a similar thing I need to set with the re-ordering of the table view for it to save? 我需要对表视图的重新排序进行设置以保存它吗?

The data is fetched from a Core Data database. 从核心数据数据库中获取数据。

In order to persist the order of the objects I'm afraid you will need to add an attribute for that to your data model . 恐怕为了保持对象的顺序,恐怕您需要为该数据模型 添加一个属性 Just use an int / [NSNumber] and recompute them in a loop when a table view row is moved. 只需使用int / [NSNumber]并在移动表视图行时循环重新计算它们即可。

When you reload the data, are you re-populating that array with the result of a fetch from your Core Data entity? 重新加载数据时,是否要使用从Core Data实体中获取的结果重新填充该数组? If so, the changes you made to the array (which I assume is the data source for the TV) will be blown away. 如果是这样,您对阵列所做的更改(我认为是电视的数据源)将被吹走。 If you want those changes to stick, you would need to update the model somehow to make sure the re-population of the array results in the array being indexed the way you need. 如果要保留这些更改,则需要以某种方式更新模型,以确保重新填充数组会导致按所需方式对数组进行索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM