问题
i have a table with 3 section (section one, section two and section three). I would like implement a mechanism to change a cell's position with drag & drop (inside same section or from a section to another).
Example 1: In section one i have two rows (sec1_row1 and sec1_row2). In section two i have one row (sec2_row1) by drag & drop function i would like invert the sec1_row2 with sec2_row1. I would like select the sec2_row1, drag it on sec2_row1 and drop it so the rows are inverted.
I would like that this feature will be also available for rows of same section.
What is the best way to implement this feature?
Thanks in advance.
回答1:
Check this apple doc link
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/ManageReorderRow/ManageReorderRow.html
you need to manage conditions in
targetIndexPathForMoveFromRowAtIndexPath
canMoveRowAtIndexPath
回答2:
- (NSIndexPath *)tableView:(UITableView *)tableView
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
NSDictionary *section = [data objectAtIndex:sourceIndexPath.section];
NSUInteger sectionCount = [[section valueForKey:@"content"] count];
if (sourceIndexPath.section != proposedDestinationIndexPath.section)
{
NSUInteger rowInSourceSection =
(sourceIndexPath.section > proposedDestinationIndexPath.section) ?
0 : sectionCount - 1;
return [NSIndexPath indexPathForRow:rowInSourceSection inSection:sourceIndexPath.section];
}
else if (proposedDestinationIndexPath.row >= sectionCount)
{
return [NSIndexPath indexPathForRow:sectionCount - 1 inSection:sourceIndexPath.section];
}
// Allow the proposed destination.
return proposedDestinationIndexPath;
}
For further information check below link:
Managing the Reordering of Rows
If you want to prevent re-ordering outside section then please refer this link: How to limit UITableView row reordering to a section
回答3:
You can control the movement of rows within section and between section using below approach
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
// Do not allow any movement between section
if ( sourceIndexPath.section != proposedDestinationIndexPath.section)
return sourceIndexPath;
// You can even control the movement of specific row within a section. e.g last row in a Section
// Check if we have selected the last row in section
if ( sourceIndexPath.row < sourceIndexPath.length) {
return proposedDestinationIndexPath;
} else {
return sourceIndexPath;
}
// You can use this approach to implement any type of movement you desire between sections or within section
}
回答4:
Try this code is working fine.
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *NameArray;
}
@property (weak, nonatomic) IBOutlet UITableView *NameListTableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_NameListTableView.delegate=self;
_NameListTableView.dataSource=self;
NameArray=[NSMutableArray arrayWithObjects:@"apple", @"b", @"c", @"d", @"e",@"f", @"g", @"h",@"i",@"j", @"k", nil];
[self.NameListTableView setEditing:YES animated:YES];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return NameArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId=@"NameCell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId];
if(cell!=nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
}
NSString *abc=[NameArray objectAtIndex:indexPath.row];
cell.textLabel.text=abc;
return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
[NameArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
[self.NameListTableView reloadData];
}
@end
来源:https://stackoverflow.com/questions/19585736/uitableview-move-cell-to-a-position-to-another-with-drag-drop