Ios 如何将UITableViewCell拖放到另一个UITableView?
我只能得到部分输出,拖放的主要功能已经实现,但它有时会崩溃,并显示错误,因为索引超出范围 我使用“平移手势控制器”拖放UITableView单元格,我还想知道如何使用“触动开始”、“触动移动”和“触动结束”方法来完成 这是我的示例代码Ios 如何将UITableViewCell拖放到另一个UITableView?,ios,iphone,ios5,ios4,Ios,Iphone,Ios5,Ios4,我只能得到部分输出,拖放的主要功能已经实现,但它有时会崩溃,并显示错误,因为索引超出范围 我使用“平移手势控制器”拖放UITableView单元格,我还想知道如何使用“触动开始”、“触动移动”和“触动结束”方法来完成 这是我的示例代码 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { pathNumber=indexPath.row; cell
- (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");
}
}
}
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nsindepath*)indepath
{
pathNumber=indexPath.row;
单元格=[tableView CellForRowatineXpath:indexPath];
UIPangEstureRecognitzer*PanRecognitzer=[[UIPangEstureRecognitzer alloc]initWithTarget:self action:@selector(panDetected:)];
[小区地址识别器:装甲识别器];
if(cell.accessoryType==UITableViewCellAccessoryNone)
{
cell.accessoryType=UITableViewCellAccessoryCheckmark;
如果(tableView==tableView1)
{
[selectedCells addObject:[array objectAtIndex:indexPath.row]];
NSLog(@“indexpath-%d”,indexpath.row);
[arrayOfPaths addObject:indexPath];
NSLog(@“---%@”,所选单元格);
NSLog(@“----====%@”,数组路径);
}
其他的
{
[selectedCells1 addObject:[array1 objectAtIndex:indexPath.row]];
NSLog(@“indexpath-%d”,indexpath.row);
[arrayOfPaths1 addObject:indexPath];
NSLog(@“==%@”,所选单元格1);
NSLog(@“----==================]%@”,数组路径1);
}
}
其他的
{
cell.accessoryType=UITableViewCellAccessoryNone;
如果(tableView==tableView1)
{
[selectedCells removeObject:[array objectAtIndex:indexPath.row]];
[arrayOfPaths removeObject:indexPath];
NSLog(@“---%@”,所选单元格);
NSLog(@“----=======remove%@”,数组路径);
}
其他的
{
[selectedCells1 removeObject:[array1 objectAtIndex:indexPath.row]];
[arrayOfPaths1 removeObject:indexPath];
NSLog(@“==%@”,所选单元格1);
NSLog(@“----=======remove1%@),ArrayOfPath1);
}
}
}
-(无效)panDetected:(UIPangestureRecognitizer*)装甲识别器
{
CGPoint translation=[panRecognizer translationView:self.view];
CGPoint cellViewPosition=cell.center;
cellViewPosition.x+=translation.x;
cellViewPosition.y+=translation.y;
cell.center=cellViewPosition;
[self.view将子视图带到前面:单元格];
[装甲识别器setTranslation:CGPointZero-inView:self.view];
NSArray*arr=[NSArray arrayWithObject:[NSIndexPath indexPathForRow:pathNumber INSTITION:0]];
NSArray*arr1=[NSArray arrayWithObject:[NSIndexPath indexPathForRow:pathNumber分区:0]];
if(panRecognizer.state==UIgestureRecognitizerStateStart)
{
位置1=[装甲识别器位置查看:self.view];
}
if(PanRecognitizer.state==UIgestureRecognitizerStateEnded)
{
位置=[装甲识别器位置查看:self.view];
NSLog(@%f%f),位置x,位置y);
if(CGRectContainsPoint(tableView2.frame,位置))
{
NSLog(@“1-2”);
如果(position.x>=tableView2.frame.origin.x&&position1.x>=tableView1.frame.origin.x&&position1.x=tableView2.frame.origin.x&&position1.x,您可以得到您正在触摸的单元格。当您拥有该单元格时,您可以存储
它们在临时单元格上的值,您必须在“.h”上声明并从中删除
第一个表的数据源
当您检测到该触摸已结束时,如果该触摸在
表,将临时单元格添加到其数据源,并重新加载数据。
否则,如果触摸未在其中一个表边界上结束,请添加临时单元格
回到原来的桌子上
为了提高视觉功能,您可以在触摸坐标上显示UITableViewCell
因此,一步一步:
- 在ViewController中声明:
UITableviewCell*tempCell
- 使用
tempCell=[[UITableViewCell alloc]Init]
- 用这种方法:
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nsindepath*)indepath
{
//在这里您已经选择了行号;
}
您可以获取所选行,以便从数据源恢复单元格值。
将该值存储在“tempCell”上,并将其从表的数据源中删除
- 如果要在触摸时显示单元格,可以添加到ViewController.view UITableViewCell
- 使用
-(void)touchesend:(NSSet*)触碰event:(UIEvent*)event;
您可以获得触碰坐标。使用这些坐标,您可以检查它们是否在一个表上。
如果坐标属于您的一个表,请添加到它们的数据源中,清除“tempCell”(如果在ViewController的视图上显示,请删除cellView)
最后执行[tableView reloadData];
。
否则,如果坐标不属于您的表,请将单元格添加到原始数据源并清除“tempCell”
对不起,我的英语不好:(您可以对单元格进行快照,然后填写imageview。
然后从tableview中删除该单元格。
将imageview添加到superview、覆盖触摸事件或添加平移手势
准备好删除snapshot imageview时,将单元格插入新的tableview并填充数据