Jon*_*Jon 5 iphone objective-c uitableview
我正在尝试使我的UITableView可编辑,以便您可以移动单元格.现在当我点击编辑按钮时,它只允许我删除但不重新安排.
我的方法是:
Code:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
[self.routineTableView setEditing: !self.routineTableView.editing animated:YES];
if (self.routineTableView.editing)
[self.navigationItem.leftBarButtonItem setTitle:@"Done"];
else
[self.navigationItem.leftBarButtonItem setTitle:@"Edit"];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
[context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
NSLog(@"fetched results : \n%@\n",[self.fetchedResultsController fetchedObjects]);
NSError *error = nil;
if (![managedObjectContext save:&error])
{
// Handle the error.
}
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
Run Code Online (Sandbox Code Playgroud)
小智 8
为了正确实现重新排序行,表视图数据源应该实现如下方法:
-(BOOL)tableView:(UITableView *)tableView
canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
-(void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath;
Run Code Online (Sandbox Code Playgroud)
并且其代表可以实施:
-(NSIndexPath *)tableView:(UITableView *)tableView
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;
Run Code Online (Sandbox Code Playgroud)
此页面还提供了代码示例.
| 归档时间: |
|
| 查看次数: |
5523 次 |
| 最近记录: |