【发布时间】: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