我只能获得部分输出,实现了拖放的主要功能,但它有时会崩溃并显示错误为索引超出范围。
我使用平移手势控制器拖放 UITableView 单元格,我还想知道如何通过使用触摸开始、触摸移动和触摸结束方法来完成。
这是我的示例代码
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
pathNumber=indexPath.row;
cell = [tableView cellForRowAtIndexPath:indexPath];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDetected:)];
[cell addGestureRecognizer:panRecognizer];
if (cell.accessoryType==UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
if(tableView==tableView1)
{
[selectedCells addObject:[array objectAtIndex:indexPath.row]];
NSLog(@"indexpath-%d",indexPath.row);
[arrayOfPaths addObject:indexPath];
NSLog(@"----%@",selectedCells);
NSLog(@"----======%@",arrayOfPaths);
}
else
{
[selectedCells1 addObject:[array1 objectAtIndex:indexPath.row]];
NSLog(@"indexpath-%d",indexPath.row);
[arrayOfPaths1 addObject:indexPath];
NSLog(@"====%@",selectedCells1);
NSLog(@"----======]]]]]%@",arrayOfPaths1);
}
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
if(tableView==tableView1)
{
[selectedCells removeObject:[array objectAtIndex:indexPath.row]];
[arrayOfPaths removeObject:indexPath];
NSLog(@"----%@",selectedCells);
NSLog(@"----======remove%@",arrayOfPaths);
}
else
{
[selectedCells1 removeObject:[array1 objectAtIndex:indexPath.row]];
[arrayOfPaths1 removeObject:indexPath];
NSLog(@"====%@",selectedCells1);
NSLog(@"----======remove1%@",arrayOfPaths1);
}
}
}
- (void)panDetected:(UIPanGestureRecognizer *)panRecognizer
{
CGPoint translation = [panRecognizer translationInView:self.view];
CGPoint cellViewPosition = cell.center;
cellViewPosition.x += translation.x;
cellViewPosition.y += translation.y;
cell.center = cellViewPosition;
[self.view bringSubviewToFront:cell];
[panRecognizer setTranslation:CGPointZero inView:self.view];
NSArray * arr=[NSArray arrayWithObject:[NSIndexPath indexPathForRow:pathNumber inSection:0]];
NSArray * arr1=[NSArray arrayWithObject:[NSIndexPath indexPathForRow:pathNumber inSection:0]];
if(panRecognizer.state==UIGestureRecognizerStateBegan)
{
position1=[panRecognizer locationInView:self.view];
}
if(panRecognizer.state==UIGestureRecognizerStateEnded)
{
position = [panRecognizer locationInView:self.view];
NSLog(@"%f %f",position.x,position.y);
if(CGRectContainsPoint(tableView2.frame, position))
{
NSLog(@"1-2");
if(position.x>=tableView2.frame.origin.x && position1.x >= tableView1.frame.origin.x && position1.x <= tableView1.frame.size.width)
{
if([selectedCells count] ==1)
{
[tableView2 beginUpdates];
[tableView2 insertRowsAtIndexPaths:arr1 withRowAnimation:UITableViewRowAnimationTop];
[array1 insertObject:[array objectAtIndex:pathNumber] atIndex:pathNumber];
d=[array1 count];
[tableView2 endUpdates];
[tableView1 beginUpdates];
[tableView1 deleteRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationTop];
[array removeObjectAtIndex:pathNumber];
c=[array count];
[tableView1 endUpdates];
}
else
{
[tableView2 beginUpdates];
[tableView2 insertRowsAtIndexPaths:arrayOfPaths withRowAnimation:UITableViewRowAnimationTop];
for(int j=0;j<[arrayOfPaths count];j++)
{
NSIndexPath * tempIndexPath = (NSIndexPath *)([arrayOfPaths objectAtIndex:j]);
[array1 insertObject:[selectedCells objectAtIndex:j] atIndex:tempIndexPath.row];
}
d=[array1 count];
NSLog(@"%d", d);
[tableView2 endUpdates];
[tableView1 beginUpdates];
[tableView1 deleteRowsAtIndexPaths:arrayOfPaths withRowAnimation:UITableViewRowAnimationTop];
for(int j = 0;j < [arrayOfPaths count]; j++)
{
NSIndexPath * temp = (NSIndexPath *)[arrayOfPaths objectAtIndex:j];
[array removeObjectAtIndex:temp.row];
}
c=[array count];
[tableView1 endUpdates];
}
[selectedCells removeAllObjects];
[arrayOfPaths removeAllObjects];
}
else
{
NSLog(@"else");
}
}
else if(CGRectContainsPoint(tableView1.frame, position))
{
NSLog(@"hi-2");
if(position.x>=tableView1.frame.origin.x && position1.x >= tableView2.frame.origin.x && position1.x <= tableView2.frame.size.width + tableView2.frame.origin.x)
{
if([selectedCells1 count] == 1)
{
[tableView1 beginUpdates];
[tableView1 insertRowsAtIndexPaths:arr1 withRowAnimation:UITableViewRowAnimationTop];
[array insertObject:[array1 objectAtIndex:pathNumber] atIndex:pathNumber];
c=[array count];
[tableView1 endUpdates];
[tableView2 beginUpdates];
[tableView2 deleteRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationTop];
[array1 removeObjectAtIndex:pathNumber];
d=[array1 count];
[tableView2 endUpdates];
}
else
{
[tableView1 beginUpdates];
[tableView1 insertRowsAtIndexPaths:arrayOfPaths1 withRowAnimation:UITableViewRowAnimationTop];
for(int j=0;j<[arrayOfPaths1 count];j++)
{
NSIndexPath * tempIndexPath = (NSIndexPath *)([arrayOfPaths1 objectAtIndex:j]);
[array insertObject:[selectedCells1 objectAtIndex:j] atIndex:tempIndexPath.row];
}
c=[array count];
NSLog(@"%d", c);
[tableView1 endUpdates];
[tableView2 beginUpdates];
[tableView2 deleteRowsAtIndexPaths:arrayOfPaths1 withRowAnimation:UITableViewRowAnimationTop];
for(int j = 0;j < [arrayOfPaths1 count]; j++)
{
NSIndexPath * temp = (NSIndexPath *)[arrayOfPaths1 objectAtIndex:j];
[array1 removeObjectAtIndex:temp.row];
}
d=[array1 count];
[tableView2 endUpdates];
}
[selectedCells1 removeAllObjects];
[arrayOfPaths1 removeAllObjects];
}
else
{
NSLog(@"else1");
}
}
}
}