1

I am looking to customize an AQGridViewCell by adding a title, date, and image for each cell.

What I have tried so far is:

//View Controller

- (AQGridViewCell *) gridView: (AQGridView *) gridView cellForItemAtIndex: (NSUInteger) index
{
    static NSString * CellIdentifier = @"CellIdentifier";

    IssueCell * cell = (IssueCell *)[self.gridView dequeueReusableCellWithIdentifier: CellIdentifier];
    if ( cell == nil )
    {
        cell = [[IssueCell alloc] initWithFrame: CGRectMake(0.0, 0.0, 72.0, 72.0) reuseIdentifier: CellIdentifier];
    }

    //This model object contains the title, picture, and date information
    IssueModel *m = (IssueModel *)[self.issues objectAtIndex:index];
    [cell setIssueModel:m];
    return cell;
}

//Cell class

#import "IssueCell.h"
#import <QuartzCore/QuartzCore.h>

@implementation IssueCell
@synthesize issueModel;

- (id) initWithFrame: (CGRect) frame reuseIdentifier:(NSString *) reuseIdentifier
{
    self = [super initWithFrame: frame reuseIdentifier: reuseIdentifier];
    if ( self == nil )
        return ( nil );


    self.contentView.backgroundColor = [UIColor redColor];
    self.backgroundColor = [UIColor blueColor];

    self.contentView.opaque = NO;
    self.opaque = NO;

    self.selectionStyle = AQGridViewCellSelectionStyleNone;

    return self;
}
@end

My questions is, since init is called before I have access to the model object, where can I setup the title, picture, and date for my cell?

Chris Muench
  • 17,444
  • 70
  • 209
  • 362

1 Answers1

1

You have to initialize your UI components in the initWithFrame. Example:

In the interface of your IssueCell add UI variables you would like to have:

@interface IssueCell : AQGridViewCell {

     UIImageView *im;
     UILabel *dateLabel;
}

- (id) initWithFrame: (CGRect) frame reuseIdentifier:(NSString *) reuseIdentifier
{
    self = [super initWithFrame: frame reuseIdentifier: reuseIdentifier];
    if ( self == nil )
        return ( nil );

    self.contentView.backgroundColor = [UIColor redColor];
    self.backgroundColor = [UIColor blueColor];

    self.contentView.opaque = NO;
    self.opaque = NO;

    self.selectionStyle = AQGridViewCellSelectionStyleNone;

    im = [[UIImageView alloc] initWithFrame:yourImageViewFrameHere];
    dateLabel = [[UILabel alloc] initWithFrame:yourLabelFrameHere];

    [self addSubview:im];
    [self addSubview:dateLabel];

    return self;
}
@end

Later, you assign desired values in the cellForItemAtIndex method. Example:

- (AQGridViewCell *) gridView: (AQGridView *) gridView cellForItemAtIndex: (NSUInteger) index
{
    static NSString * CellIdentifier = @"CellIdentifier";

    IssueCell * cell = (IssueCell *)[self.gridView dequeueReusableCellWithIdentifier: CellIdentifier];
    if ( cell == nil )
    {
        cell = [[IssueCell alloc] initWithFrame: CGRectMake(0.0, 0.0, 72.0, 72.0) reuseIdentifier: CellIdentifier];
    }

    //This model object contains the title, picture, and date information
    //
    IssueModel *m = (IssueModel *)[self.issues objectAtIndex:index];
    [cell.im setImage: m.picture];
    [cell.dateLabel setText:[date localizedDescription]];
    return cell;
}

Do not store your model data in the UI components. That's a no no. Keep your model separated from the UI. This is only a pseudocode, not tested since I do not have my mac here.

Let me know if it helps.

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
  • Would you by chance know how to do the interface in IB? I have been trying to figure it out at http://stackoverflow.com/questions/5589575/load-a-aqgridviewcell-from-xib-not-working – Chris Muench Dec 14 '11 at 20:51
  • I think I kind of figured out but not sure why. It seems that ReusableGridViewCell's file owner is ReusableGridViewCellExampleViewController which makes the outlet connection. – Chris Muench Dec 14 '11 at 22:00