【问题标题】:UICollectionView move specific cellUICollectionView 移动特定单元格
【发布时间】:2014-03-02 23:23:13
【问题描述】:

我有一个 UICollectionView 的工作示例,我可以移动一个单元格 [by indexPath],但我需要一个特定的单元格 [indexpath.row?] 所以我移动了我需要的单元格,此时一个单元格移动但不是正确一个,所以 例如,当我移动时

NSIndexPath *index = [indexPaths objectAtIndex:0];

indexpath.row = 3 上的单元格移动,如何按 indexpath 行调用单元格?

- (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;
}

所以一个单元格移动了,但不是我想要的那个,我需要通过 indexpath.row 移动一个特定的单元格,就像它在 cellForRow 上显示的那样,

如何指定要移动的单元格?

请注意,我在 index = 0 上移动单元格,但 indexpath.row = 2 是移动单元格

谢谢

【问题讨论】:

    标签: ios uicollectionview nsindexpath


    【解决方案1】:

    你尝试过这样的事情吗?

    [NSIndexPath indexPathForItem:0 inSection:0]
    

    【讨论】:

      【解决方案2】:

      首先,如果不使用数组作为数据源,对 UICollectionView 进行任何调整会更加困难。最常见的是,您将拥有一个保存数据的数组,并且在您的 cellForItemAtIndexPath: 方法中,您可以通过以下方式从该数组中获取特定于索引的数据:

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

      这样,您可以使用与当前 indexPath 相关的数据填充单元格。一旦你这样做了,只需更新你的数据数组,然后调用:

      [self.collectionView reloadData];
      

      然后更新将在集合视图中表示。如果您想为这些更改设置动画,请使用:

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

      如果您想“移动”单元格,使其外观改变其原始位置,您可以管理您的arrayOfMyData,在您希望为空的索引处添加一个指示符,以将单元格的背景设置为清晰并放置要在数组末尾移动的单元格,然后调用 reloadData。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多