UICollectionViewのCellのカスタムセルをXibで作りました。
CustomCollectionViewCell.xibとSecondCollectionViewCell.xibの2つです。
カスタムセルのCustomCollectionViewCell.xibにはUIButtonを配置して、タップしたらSecondCollectionViewCell.xibにビューを入れ替えたいと思っています。
ボタンをタップしたら、indexPathを精査して、データソースのフラグをYesにしてreloadDataを行えば、ビューの入れ替えをすることができました。
このXibのビューの入れ替えをアニメーション付きで行いたいのですが、
どのような方向性で実装すればいいのかが検討つかない状態です。
お知恵をお借りしたいと思うのでよろしくお願い致します。
lang
1@interface ViewController () 2@property (weak, nonatomic) IBOutlet UICollectionView *myCollection; 3@property NSMutableArray *dataArray; 4 5@end 6@implementation ViewController 7 8- (void)viewDidLoad { 9 [super viewDidLoad]; 10 // Do any additional setup after loading the view, typically from a nib. 11 12 self.myCollection.dataSource = self; 13 self.myCollection.delegate = self; 14 15 self.dataArray = [NSMutableArray array]; 16 17 for (int i = 0; i < 9; i++) { 18 19 NSMutableDictionary *dic = [@{ 20 @"isSelected" : [NSNumber numberWithBool:NO], 21 @"buttonText" : @"tap me !" 22 } mutableCopy]; 23 24 [self.dataArray addObject:dic]; 25 } 26//Xibの登録 27 [self.myCollection registerNib:[UINib nibWithNibName:@"CustomCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"samplecell"]; 28 [self.myCollection registerNib:[UINib nibWithNibName:@"SecondCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"secondCell"]; 29} 30 31- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 32 return [self.dataArray count]; 33} 34 35- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 36 37//データソースの配列をみて、isSelectedの真偽値によってxibのセルを分ける 38 UICollectionViewCell *cell = nil; 39 40 BOOL b = [[self.dataArray[indexPath.row] objectForKey:@"isSelected"] boolValue]; 41 42 43 SecondCollectionViewCell *secondcell = [collectionView dequeueReusableCellWithReuseIdentifier:@"secondCell" forIndexPath:indexPath]; 44 secondcell.backgroundColor = [UIColor redColor]; 45 46 47 CustomCollectionViewCell *firstcell = [collectionView dequeueReusableCellWithReuseIdentifier:@"samplecell" forIndexPath:indexPath]; 48 firstcell.backgroundColor = [UIColor greenColor]; 49 ; 50 NSString *str =[self.dataArray[indexPath.row] objectForKey:@"buttonText"]; 51 52 firstcell.buttonInCell.titleLabel.text = str; 53 54 [firstcell.buttonInCell addTarget:self action:@selector(handleTouchButton: event:) forControlEvents:UIControlEventTouchUpInside]; 55 56 57 if (!secondcell && !firstcell) { 58 return cell; 59 }else if (b){ 60 return secondcell; 61 62 }else{ 63 return firstcell; 64 } 65} 66 67- (CGSize)collectionView:(UICollectionView *)collectionView 68 layout:(UICollectionViewLayout*)collectionViewLayout 69 sizeForItemAtIndexPath:(NSIndexPath *)indexPath; 70{ 71//データソースの"isSelected"によって高さを変化させる 72 CGSize cellSize = CGSizeMake(self.myCollection.frame.size.width, 44); 73 74 75 NSNumber *isSecelecell = [self.dataArray[indexPath.row] objectForKey:@"isSelected"]; 76 77 BOOL isSe = [isSecelecell boolValue]; 78 79 if (isSe) { 80 cellSize = CGSizeMake(self.myCollection.frame.size.width, 156); 81 82 } 83 return cellSize; 84} 85 86- (void)handleTouchButton:(UIButton *)sender event:(UIEvent *)event { 87//ボタンタップでそのセルのindexPathを取得 88 NSIndexPath *selectedIndexPath = nil; 89 selectedIndexPath = [self.myCollection indexPathForItemAtPoint:[self.myCollection convertPoint:sender.center fromView:sender.superview]]; 90 NSNumber *nsBool = [self.dataArray[selectedIndexPath.row] valueForKey:@"isSelected"]; 91 92 93 BOOL b = [nsBool boolValue]; 94 95 if (!b) { 96 b = YES; 97 [self.dataArray[selectedIndexPath.row] setObject:[NSNumber numberWithBool:b] forKey:@"isSelected"]; 98 } 99 100 [self.myCollection reloadData]; 101 102} 103 104
あなたの回答
tips
プレビュー