Making a header in a UICollectionView is turning out to be much harder then in a table.
I subclassed the UICollectionViewReusableView
, registered it's class in the UICollectionView
, implemented collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
. In my header view I have two UITextField
's and one UITextView
. I need to have access to their delegate methods (editing began, finished...). Using this example I saw that you can do:
UICollectionReusableView *footer = (UICollectionReusableView*)[self.collectionView viewWithTag:999];
UILabel *footerLabel = (UILabel*)[footer viewWithTag:100];
But creating that many variables for every delegate method seems wrong. I need to be able to set the text in those text fields and text view. Is there a better way of doing this?
//...viewDidLoad
UICollectionView *images = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
[images registerClass:[ImagesCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[images registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
//images data source and delegate
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
if (kind == UICollectionElementKindSectionHeader) {
HeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
if (!headerView)
headerView = [[HeaderView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 180)];
headerView.delegate = self;
return headerView;
}
return nil;
}
And then I use casting with the tags to get the view. Is there any other way to update the individual views inside the header?
UPDATE:
HeaderView.h
@protocol HeaderViewDelegate <NSObject>
- (void)headerView:(id)view didBeginEditingFullName:(UITextField *)fullNameTextField;
- (void)headerView:(id)view didEndEditingFullName:(UITextField *)fullNameTextField;
- (void)headerView:(id)view didBeginEditingBreed:(UITextField *)breedTextField;
- (void)headerView:(id)view didEndEditingBreed:(UITextField *)breedTextField;
- (void)headerView:(id)view didBeginEditingDescription:(UITextView *)descriptionTextView;
- (void)headerView:(id)view didEndEditingDescription:(UITextView *)descriptionTextView;
@end
@interface HeaderView : UICollectionReusableView {
id <HeaderViewDelegate> delegate;
}
@property (nonatomic, weak) id <HeaderViewDelegate> delegate;
This is how I set up the HeaderView.h...had to use id
instead of (HeaderView *)
in the methods...I was getting an error.
This is what I had in my PersonalViewController.m page before:
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if (textField == _fullName) {
}else if (....)
}
Now that _fullName
is inside the supplementary view, I can't access it. This is what I was trying to get to...
Changing it to this:
- (void)headerView:(HeaderView *)view didBeginEditingFullName:(UITextField *)fullName{
NSLog (@"here");
}
No log output. I set the delegates for the views in HeaderView.m. Dammit, what am I not getting here?
UPDATE:
HeaderView.h is the same as before. I added the textfield and textview delegates to it. HeaderView.m:
@implementation HeaderView
@synthesize delegate=_delegate;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
UITextField *fullName = [[UITextField alloc]initWithFrame:CGRectMake(5, 100, 120, 21)];
fullName.tag = 4;
fullName.delegate = self;
_fullName = fullName;
[self addSubview:_fullName];
}
}
PersonalViewController.m is the same. viewForSupplementaryElementOfKind
is still the same as before...setting headerView.delegate = self;
. In it I'm calling
- (void)headerView:(HeaderView *)view didBeginEditingFullName:(UITextField *)fullName{
NSLog(@"here");
}
Should I be calling it inside the HeaderView.m? I need to call it from PersonalViewController.m...