3

I am using AQGridView class and I am trying to load a cell from an XIB. I have setup the XIB like a Custom Cell for a UITableView, but when I attempt to load the cell, it is simply blank. I was wondering if there was an easier way to get the XIB to load.

AQGridViewCell need to load the cell from an xib

- (AQGridViewCell *) gridView: (AQGridView *) gridView cellForItemAtIndex: (NSUInteger) index
{
    static NSString * CellIdentifier = @"cellID";
    gridCell * cell = (gridCell *)[gridView dequeueReusableCellWithIdentifier: CellIdentifier];
    if ( cell == nil ){
        gridCell = [[gridViewCell alloc] initWithFrame: CGRectMake(0,0,_gridView.frame.size.width/2-4, 
                                                                       _gridView.frame.size.height/2-8) 
                                       reuseIdentifier:CellIdentifier];
        cell = gridCell;
        self.gridCell = nil;
    }

    cell.title = @"Test Grid Item";
    cell.date  = @"Apr. 7, 2011";

    return ( cell );
}
WrightsCS
  • 50,551
  • 22
  • 134
  • 186

6 Answers6

7

Here's an article that describes how to load an AQGridViewCell from nib, with example code. Check out the section called "A reusable AQGridViewCell".

(Thanks to pt2ph8 for pointing out contentView.)

jlstrecker
  • 4,953
  • 3
  • 46
  • 60
  • I am having a hard time understanding why this works. In ReusableGridViewCellExampleViewController.h there is an outlet "gridViewCellContent", but it doesn't seem to be hooked up (and there is nothing available to hook it up to...) Yet it works correctly. Could you point me in the right direction to why this works? – Chris Muench Dec 14 '11 at 20:40
2

This took me a while, but I figured a different way than the blog post jlstrecker mentioned.

  1. Create a subclass of AQGridViewCell - let's call it MyGridViewCell.
  2. Create a nib for that cell, link it up in IB.
  3. Pub a view ON TOP of the cell's view in IB. That's right, a view on top of a view. Make the size the exact same.
  4. For that view on top of the view (let's call it view2), set the tag property (can be done in IB) to 1.
  5. Put everything you want to link up on top of view2, decorate your cell, whatever you'd like.
  6. Use the following code (of course, change it to your needs) in your subclass of AQGridViewController:

`

- (AQGridViewCell *)gridView:(AQGridView *)aGridView cellForItemAtIndex:(NSUInteger)index {
    static NSString *CellIdentifier = @"MyGridViewCell";
    MyGridViewCell *cell = (MyGridViewCell *)[self.gridView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = (ZZProductGridViewCell *)[[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
    }

    [cell.contentView addSubview:[cell viewWithTag:1]]; //THIS IS THE IMPORTANT PART

    return cell;
}

Enjoy!

Baub
  • 5,004
  • 14
  • 56
  • 99
2

From what I've understood, I think it shows as blank because what gets displayed is the cell's contentView. I ended up loading my custom view from IB and adding it as a subview of the cell's contentView when the cell is requested.

AQGridView's developers once claimed on GitHub that proper IB support will be added in the future, but that post is dated August 2010, so don't hold your breath.

ySgPjx
  • 10,165
  • 7
  • 61
  • 78
0

Build your cell normally using IB, then in your subclass of AQGridViewCell, add

- (void)awakeFromNib{
    self.contentView.backgroundColor = [UIColor clearColor];
}
Gia Dang
  • 164
  • 3
  • 8
0

I'm not familiar with AQGridView, but I believe you can leverage NSBundle's Nib loading capabilities. An excerpt from AdvancedTableViewCells sample project illustrates the idea:

RootViewController.h

@interface RootViewController : UITableViewController
{
    ApplicationCell *tmpCell;
}

RootViewController.m

ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
    [[NSBundle mainBundle] loadNibNamed:@"IndividualSubviewsBasedApplicationCell" owner:self options:nil];
    cell = tmpCell;
    self.tmpCell = nil;
}

Inside the IndividualSubviewsBasedApplicationCell.xib you would have to set the outlet of the UITableViewCell within to be the RootViewController's tmpCell property. Then, as a side effect of invoking NSBundle's loadNibNamed method, the tmpCell property gets set on the RootViewController via the Nib loading mechanism.

Damian Carrillo
  • 1,218
  • 9
  • 11
  • This is how I am creating my "Custom Cell". However, the cell is blank when actually loaded. I am stumped at the moment as to why. – WrightsCS Apr 08 '11 at 02:49
  • It looks as if you are initializing the cell after you load it with the bundle loading mechanism. Try taking out your gridCell = [[gridViewCell alloc] initWithFrame:... line. – Damian Carrillo Apr 08 '11 at 03:14
  • Also, make sure to set the reuseIdentifier on the gridCell that gets loaded, otherwise there will be a penalty incurred each time a cell is needed. – Damian Carrillo Apr 08 '11 at 03:17
  • I have removed the `gridCell = [[gridViewCell alloc] initWithFrame..` but when ran, they are dont interact , and they are kind of messed up. – WrightsCS Apr 08 '11 at 03:21
  • The only thing I can suggest is that you ensure that the the actual object that gridCell gets populated with is an actual instance of an AQGridViewCell or a subclass of it. In your Xib file you will have an object that represents the cell, and you must specify that the implementation class of that cell object is a type of AQGridViewCell. That's the only other suggestion I can provide without knowing a bit more about your controller class. – Damian Carrillo Apr 08 '11 at 04:01
0

What you can do is do your xib (uiview) unpacking/loading in the subclass itself (which does have a different init method than a uitableviewcell) you can also connect any outlets to this xib and add its entire view as a subview, or maybe replace contentview).

To make it even faster you can make uinib of this xib and reuse it to save disk i/o.

drunknbass
  • 1,662
  • 1
  • 13
  • 19