我一直在努力解决我在这里做错了什么。我的目标是能够拖动CollectionViewCell并让它用拖动的单元替换目标单元格,同时让替换的单元格接管最初拖动的单元格上的位置。这甚至让我感到困惑!我可以得到拖动和细胞替换,但我的问题是,当我发生时,我会在集合中留下越来越多的空单元格。
我对目标C来说相当新,所以很可能我在这里错过了一些愚蠢的东西..任何帮助都会非常感激。希望我已经包含足够的相关代码来帮助和链接图像以显示我的问题...
Cheers Leigh
- (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)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [playerPositionsARR count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
OBJ_Positions *s = [playerPositionsARR objectAtIndex:indexPath.row];
CVC_PlayerPositions *pcell = (CVC_PlayerPositions *)[collectionView dequeueReusableCellWithReuseIdentifier:@"PositionCell" 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)moveItemAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)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 insertObject:oldPlayer atIndex:indexPath.row];
//REMOVE ADDED POSITION OBJECT FROM INSERTION
[playerPositionsARR removeObjectAtIndex:indexPath.row +1];
[CV_playerPositions reloadData];
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
OBJ_Positions *p = [playerPositionsARR objectAtIndex:indexPath.row];
NSLog(@"Selected Player IS %@",p.playerName);
}
#pragma mark - MOVE COLLECTION VIEW CELLS
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return YES;
}
-(void)dragPlayer:(UILongPressGestureRecognizer *)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 cellForItemAtIndexPath:originalCellIndex];
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 objectAtIndex:originalCellIndex.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(@"POSITION IS - %@ AND PLAYER DRAGGED IS %@",p.position,p.playerName);
[self moveItemAtIndexPath:originalCellIndex toIndexPath:endCellIndex];
[beginView removeFromSuperview];
return;
}
}
}
}
- (IBAction)movingPlayer:(id)sender {
[self dragPlayer:sender];
}
答案 0 :(得分:1)
我相信这个链接正是你要找的。 https://github.com/lxcid/LXReorderableCollectionViewFlowLayout
它扩展UICollectionViewFlowLayout
以支持细胞的重新排序。类似于在iBook中长按和平移书籍。