dev gr dev gr - 1 year ago 108
iOS Question

Override delegate and datasource setters in UICollectionView subclass

I created a subclass of

and in that subclass I want to override setters of

It uses another
properties (which can be set by caller) to populate the data. I want to disallow setting original dataSource and delegate properties by the caller because subclass itself is the dataSource and delegate.

How can I do that?

Below is what I did but it's throwing an error : Data source is not set.

Interface file:

@class ZISegmentCollectionView;

@protocol ZISegmentCollectionViewDelegate <NSObject>

-(void)segmentCollectionView:(ZISegmentCollectionView *)collectionView didSelectSegmentWithName:(NSString *)segmentName;


@protocol ZISegmentCollectionViewDataSource <NSObject>

-(NSUInteger)segmentCollectionView:(ZISegmentCollectionView *)collectionView badgeCountForSegment:(NSString *)segmentName;
-(NSString *)segmentCollectionView:(ZISegmentCollectionView *)collectionView nameForSegmentAtIndexPath:(NSIndexPath *)indexPath;
-(NSUInteger)numberOfSegmentsInSegmentCollectionView:(ZISegmentCollectionView *)collectionView;


@interface ZISegmentCollectionView : UICollectionView

@property(nonatomic, readonly) NSString * selectedSegmentName;
@property(nonatomic, weak) id<ZISegmentCollectionViewDelegate> segmentSelectionDelegate;
@property(nonatomic, weak) id<ZISegmentCollectionViewDataSource> segmentDataSource;


I synthesised delegate and dataSource properties in

@synthesize dataSource = _dataSource;
@synthesize delegate = _delegate;

Wrote setters like:

if (dataSource == self) {
_dataSource = dataSource;

if (delegate == self) {
_delegate = delegate;

Avi Avi
Answer Source

What you did by synthesizing the properties in your subclass is create new instance variables. When the superclass code checks if the data source is set, the answer is no, because its instance variable is still nil.

Drop the synthesize statements, because you will be implementing the setter anyway (and you don't need to provide a getter).

In your overriding setter, do nothing.

To set the actual data source, invoke [super setDatasource:self].

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download