从 XIB 加载 AQGridViewCell (不工作)

发布于 2024-10-31 06:15:54 字数 1030 浏览 1 评论 0原文

我正在使用 AQGridView 类,我试图从 <强>XIB。我已经将 XIB 设置为 UITableView 的自定义单元格,但是当我尝试加载该单元格时,它只是空白。我想知道是否有更简单的方法来加载 XIB

AQGridViewCell 需要从 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 );
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

去了角落 2024-11-07 06:15:54

这里有一篇文章,介绍了如何从 nib 加载 AQGridViewCell 以及示例代码。查看名为“可重用的 AQGridViewCell”的部分。

(感谢 pt2ph8 指出 contentView。)

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

我的痛♀有谁懂 2024-11-07 06:15:54

据我了解,我认为它显示为空白,因为显示的是单元格的 contentView。我最终从 IB 加载了自定义视图,并在请求单元格时将其添加为单元格 contentView 的子视图。

AQGridView 的开发人员曾在 GitHub 上声称将来会添加适当的 IB 支持,但该帖子的日期是 2010 年 8 月,所以不要屏住呼吸。

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.

甜味拾荒者 2024-11-07 06:15:54

这花了我一段时间,但我想出了一种与博客文章 jlstrecker 提到的不同的方法。

  1. 创建 AQGridViewCell 的子类 - 我们称之为
    MyGridViewCell
  2. 为该单元创建一个笔尖,在 IB 中将其链接起来。
  3. 在 IB 中单元视图的顶部发布一个视图。没错,一个观点
    在视图之上。使尺寸完全相同。
  4. 对于这个观点
    在视图的顶部(我们称之为 view2),设置 tag 属性(可以
    在IB中完成)到1.
  5. 把你想要链接的所有东西放在上面
    view2,装饰你的单元格,无论你想要什么。
  6. 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!

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!

临风闻羌笛 2024-11-07 06:15:54

我对 AQGridView 不熟悉,但我相信您可以利用 NSBundle 的 Nib 加载功能。 AdvancedTableViewCells 示例项目的摘录说明了这个想法:

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

在IndividualSubviewsBasedApplicationCell.xib 内部,您必须将UITableViewCell 的出口设置为RootViewController 的tmpCell 属性。然后,作为调用 NSBundle 的 loadNibNamed 方法的副作用,tmpCell 属性通过 Nib 加载机制在 RootViewController 上设置。

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.

浅唱ヾ落雨殇 2024-11-07 06:15:54

您可以做的是在子类本身中进行 xib (uiview) 解包/加载(它确实具有与 uitableviewcell 不同的 init 方法)
您还可以将任何插座连接到此 xib 并将其整个视图添加为子视图,或者替换 contentview)。

为了使其更快,您可以将此 xib 制作为 uinib 并重用它来节省磁盘 I/O。

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.

長街聽風 2024-11-07 06:15:54

通常使用 IB 构建单元格,然后在 AQGridViewCell 的子类中添加

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

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

- (void)awakeFromNib{
    self.contentView.backgroundColor = [UIColor clearColor];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文