Load a AQGridViewCell from XIB (not working)

2019-05-15 12:28发布

问题:

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 );
}

回答1:

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.)



回答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.



回答3:

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!



回答4:

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.



回答5:

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.



回答6:

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

- (void)awakeFromNib{
    self.contentView.backgroundColor = [UIColor clearColor];
}