質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Q&A

0回答

2075閲覧

UICollectionviewCellをアニメーション付きでxibを入れ替えたい

SatoTakeshiX

総合スコア113

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

0グッド

0クリップ

投稿2015/02/26 07:51

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問