Kunal Kumar
Kunal Kumar

Reputation: 1722

Adding Button and label to UICollectionView cell

Suppose I am developing or say prototyping an e-commerce app, where I have to show product list with their price-tag(it will be UILabel), product description(in UILabel) and buy(UIButton) button. Product images will come from the web-server and it can be thousands.

So, I have chosen UICollectionView over UITableview, for more appealing looks. For buy button I have added UIButton in the cell of UICollectionView. But I can't make outlet from the UILabel, it says illegal configuration. So, am I going to right direction by choosing UICollectionView to display images coming from the back-end or there is any other way to do it efficiently?

I searched a lot but didn't found any good example to integrate UICollectionView cell with buttons. For better understanding of my use case, I am attaching the screenshot.

screenshot of my requirement

Upvotes: 1

Views: 774

Answers (3)

Arun Kumar P
Arun Kumar P

Reputation: 900

Use UITableView with static cells. So you can make design in XIB and your IBOutlet connections. (Note that static cells only support for UITableViewController)

Upvotes: 0

Pulkit Sharma
Pulkit Sharma

Reputation: 555

You need to create a separate class(with xib)for the cell you are using in your collectionView.

See this tutorial, will assist you in subclassing collection view cell.

http://sledgedev.com/build-a-custom-uicollectionviewcell/

Upvotes: 1

KP_G
KP_G

Reputation: 470

Create a custom class for your cell and to handle your button's event create a delegate in it and handle it there.

@protocol CellDelegate

- (void) action;

@end

@interface CustomCell : UICollectionViewCell

@property (nonatomic, weak) id<CellDelegate> delegate;
@property (weak, nonatomic) IBOutlet UIButton *btn;

@end

Upvotes: 1

Related Questions