我在弄清楚我在这里做错了什么时遇到了一些麻烦。我的目标是能够拖动 CollectionViewCell 并让它用拖动的单元格替换目标单元格,同时让替换的单元格接管最初拖动的单元格上的位置。这甚至让我感到困惑!我可以拖动并替换单元格,但我的问题是,当我发生时,我会在集合中留下越来越多的空单元格..
我对 Objective-C 相当陌生,所以很可能我在这里遗漏了一些愚蠢的东西。任何帮助将不胜感激。希望我包含了足够的相关代码来帮助并链接图像以显示我的问题...
干杯李
Collection View Issue Image
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self getPlayerPositions];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)getPlayerPositions
{
SQL_PlayerTracker *pt = [[SQL_PlayerTracker alloc]init];
playerPositionsARR = [[pt getPositions:MD]mutableCopy];
[CV_playerPositions reloadData];
}
-(NSInteger)collectionViewUICollectionView *)collectionView numberOfItemsInSectionNSInteger)section
{
return [playerPositionsARR count];
}
-(UICollectionViewCell *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)indexPath
{
OBJ_Positions *s = [playerPositionsARR objectAtIndex:indexPath.row];
CVC_PlayerPositions *pcell = (CVC_PlayerPositions *)[collectionView dequeueReusableCellWithReuseIdentifier"ositionCell" forIndexPath:indexPath];
pcell.playerName.text = s.playerName;
pcell.positionName.text = s.position;
pcell.playerNumber.text = [NSString stringWithFormat"%@",s.playerNumber];
if (pcell.selected) {
pcell.backgroundColor = [UIColor greenColor];
}
return pcell;
}
- (void)moveItemAtIndexPathNSIndexPath *)indexPath toIndexPathNSIndexPath *)newIndexPath
{
NSLog(@"MOVED INDEX %i TO %i",indexPath.row,newIndexPath.row);
OBJ_Positions *oldPlayer = [playerPositionsARR objectAtIndex:newIndexPath.row];
OBJ_Positions *newPlayer = [playerPositionsARR objectAtIndex:indexPath.row];
//REMOVE TARGET POSITION FROM ARRAY
[playerPositionsARR removeObjectAtIndex:newIndexPath.row];
//INSERT NEW PLAYER POSITION IN TARGET POSITION
[playerPositionsARR insertObject:newPlayer atIndex:newIndexPath.row];
//INSERT OLD PLAYER IN MOVED PLAYERS POSITION
[playerPositionsARR insertObjectldPlayer atIndex:indexPath.row];
//REMOVE ADDED POSITION OBJECT FROM INSERTION
[playerPositionsARR removeObjectAtIndex:indexPath.row +1];
[CV_playerPositions reloadData];
}
-(void)collectionViewUICollectionView *)collectionView didSelectItemAtIndexPathNSIndexPath *)indexPath
{
OBJ_Positions *p = [playerPositionsARR objectAtIndex:indexPath.row];
NSLog(@"Selected Player IS %@",p.playerName);
}
#pragma mark - MOVE COLLECTION VIEW CELLS
-(BOOL)gestureRecognizerShouldBeginUIGestureRecognizer *)gestureRecognizer
{
return YES;
}
-(void)dragPlayerUILongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateBegan) {
NSLog(@"State Began");
CGPoint point = [sender locationInView:CV_playerPositions];
originalCellIndex = [CV_playerPositions indexPathForItemAtPoint:point];
if (originalCellIndex) {
UICollectionViewCell *cell = [CV_playerPositions cellForItemAtIndexPathriginalCellIndex];
beginView = [cell.contentView viewWithTag:cell.tag];
beginView.backgroundColor = [UIColor greenColor];
[beginView removeFromSuperview];
beginView.center = [CV_playerPositions convertPoint:point toView:self.view];
originalStartPoint = beginView.center;
[self.view addSubview:beginView];
[self.view bringSubviewToFront:beginView];
OBJ_Players *pd = [playerPositionsARR objectAtIndexriginalCellIndex.row];
NSLog(@"SELECT PLAYER NAME IS - %@",pd.playerName);
return;
}
}
if (sender.state == UIGestureRecognizerStateChanged) {
NSLog(@"State Changed");
if (!beginView)
return;
CGPoint location = [sender locationInView:self.view];
CGPoint translation;
translation.x = location.x - originalStartPoint.x;
translation.y = location.y - originalStartPoint.y;
CGAffineTransform theTransform = beginView.transform;
theTransform.tx = translation.x;
theTransform.ty = translation.y;
beginView.transform = theTransform;
return;
}
if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(@"STATE ENDED");
if (beginView) {
CGPoint epoint = [sender locationInView:CV_playerPositions];
endCellIndex = [CV_playerPositions indexPathForItemAtPoint:epoint];
if(endCellIndex)
{
OBJ_Positions *p = [playerPositionsARR objectAtIndex:endCellIndex.row];
NSLog(@"OSITION IS - %@ AND PLAYER DRAGGED IS %@",p.position,p.playerName);
[self moveItemAtIndexPathriginalCellIndex toIndexPath:endCellIndex];
[beginView removeFromSuperview];
return;
}
}
}
}
- (IBAction)movingPlayer:(id)sender {
[self dragPlayer:sender];
}
Best Answer-推荐答案 strong>
我相信这个链接就是你要找的。
https://github.com/lxcid/LXReorderableCollectionViewFlowLayout
它扩展了 UICollectionViewFlowLayout 以支持单元格的重新排序。类似于 iBook 中的长按和平移。
关于ios - 通过长按手势交换 UICollectionView 单元格位置,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/18738182/
|