Follow this answer on SO I be able to create UICollectionView
programmatically. But I can't find any better solution when I try to add subview into UICollectionViewCell
. Here is how most answer on SO achieve
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,100,100)];
image.image = /*some image*/
[cell addSubview:image];
return cell;
}
Maybe i'm wrong but the purpose of using UICollectionView
wasn't because of recycle and reuse to optimize the performance? If using the code above when the user scrolling wasn't it add more and more UIImageView
into UICollectionViewCell
subview when dequeueReusableCellWithReuseIdentifier
get trigger?
So what is the better way to do this? I can't use UITableView
for this because I need the horizontal scrolling technique.
NOTE: I need to create it programmatically without using xib.