2014-03-02 121 views
0

我有一個工作樣本的UICollectionView,我可以移動一個單元格[通過indexPath],但我需要一個特定的單元格[indexpath.row?]所以我移動我需要的單元格,移動,但不正確的,所以 當我移動例如UICollectionView移動特定單元格

NSIndexPath *指數= [indexPaths objectAtIndex:0];

indexpath.row = 3上的單元格移動,如何通過indexpath行調用單元格?

enter image description here

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    self.view.backgroundColor = [UIColor lightGrayColor]; 

    CGRect frame = CGRectMake(10, 100, 300, 500); 

    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init]; 
    self.collectionView=[[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout]; 
    [self.collectionView setDataSource:self]; 
    [self.collectionView setDelegate:self]; 

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"]; 
    [self.collectionView setBackgroundColor:[UIColor redColor]]; 

    [self.view addSubview:self.collectionView]; 


    UIButton *animus = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    animus.frame = CGRectMake(100, 20, 90, 30); 
    [animus setTitle:@"Animus" forState:UIControlStateNormal]; 
    [animus addTarget:self action:@selector(animusPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:animus]; 

} 

- (void)animusPressed:(id)sender 
{ 
    NSIndexPath *index = [indexPaths objectAtIndex:0]; //en el index! 

NSLog(@"moving tha cell :: %d", index.row); 


    __weak UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:index]; // Avoid retain cycles 

    CGRect frame = cell.frame; 

    frame.origin.y = 300; 

    cell.frame = frame; 
} 


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; 

    cell.backgroundColor=[UIColor greenColor]; 

    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, cell.bounds.size.width, 40)]; 
    [cell.contentView addSubview:title]; 
    NSString *titleLbl = [NSString stringWithFormat:@"i = %d", indexPath.row]; 
    title.text = titleLbl; 

    return cell; 
} 

所以小區的動作,但不是我想要的,我需要一個像它是在cellForRow顯示了對移動特定的細胞,通過indexpath.row,

如何指定我想要移動的單元格?

請注意IM在指數= 0移向細胞,但indexpath.row = 2是一個移動

感謝

回答

2

您是否嘗試過這樣的事情?

[NSIndexPath indexPathForItem:0 inSection:0] 
1

首先,不使用數組作爲數據源對UICollectionView進行任何調整會更困難。最常見的是,你將有一個數組保存數據,並在您的cellForItemAtIndexPath:方法,你通過抓住這個數組索引專用的數據:

MyData *myData = [self.arrayOfMyData objectAtIndex:indexPath.row]; 

這樣你就可以使用填充與當前indexPath數據的單元格。一旦你這樣做,只需更新您的數據數組,然後調用:

[self.collectionView reloadData]; 

然後更新將在集合視圖來表示。如果你想動畫這些變化,使用方法:

[self.collectionView performBatchUpdates:^{ 
    [self.collectionView reloadData]; 
} completion:nil]; 

如果要「移動」細胞與周圍的外觀,它改變了原來的位置,你可以管理你的arrayOfMyData到你想要的索引處添加一個能清空以將單元格的背景設置爲清晰,並將單元格移動到數組的末尾,然後調用reloadData。

相關問題