0

我使用 coredata 用表格视图中显示的数据填充数组。在表格视图中,我有两个部分。当一个单元格被推入第 1 节时,我希望将该单元格移动到第 2 节,反之亦然。

我不太确定如何做到这一点,我已经坐了大约 8 个小时试图弄清楚。

这是我到目前为止得到的:

我使用此代码来获取数据:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];

NSMutableArray *sortedIngredients = [[NSMutableArray alloc] initWithArray:[event.tags allObjects]];
[sortedIngredients sortUsingDescriptors:sortDescriptors];
self.tagsArray = sortedIngredients;

[sortDescriptor release];
[sortDescriptors release];
[sortedIngredients release];

在 didSelectRowForIndexPath 中,我弄清楚了如何删除单元格:

Tag *tag = [tagsArray objectAtIndex:indexPath.row];

NSManagedObjectContext *context = event.managedObjectContext;
[context deleteObject:tag];

[tagsArray removeObject:tag];

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

如何在 tableView 的第 2 节中插入此单元格?我应该创建 2 个数组吗?我尝试了一些使用 NSDictiorary 的代码,如下所示:

[tableView addObject: sectionOneObject forKey:@"Section1"];
[tableView addObject: sectionTwoObjects forKey: @"Section2"]; 

但我从来没有设法让它工作。

任何帮助将不胜感激!提前致谢

4

1 回答 1

0

签出-insertRowsAtIndexPaths:withRowAnimation,例如:

unsigned _insertionIndices[2] = {1,0}; // second section, first row
NSIndexPath *_insertionIndexPath = [[NSIndexPath alloc] initWithIndexes: _insertionIndices length:2];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:_insertionIndexPath] withRowAnimation: UITableViewRowAnimationRight];
[_insertionIndexPath release];
于 2009-09-27T22:20:47.760 回答