从XIB加载AQGridViewCell(不工作)

时间:2011-04-08 01:47:14

标签: ios ipad uitableview subclass aqgridview

我正在使用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 );
}

6 个答案:

答案 0 :(得分:7)

Here's an article描述了如何使用示例代码从nib加载AQGridViewCell。查看名为“可重用的AQGridViewCell”的部分。

(感谢pt2ph8指出contentView。)

答案 1 :(得分:2)

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

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

答案 2 :(得分:2)

这花了我一段时间,但我认为与jlstrecker提到的博客文章不同。

  1. 创建AQGridViewCell的子类 - 让我们调用它 MyGridViewCell
  2. 为该单元格创建一个笔尖,将其链接到IB。
  3. 在IB中查看单元格视图的顶部视图。这是正确的,一种观点 在视图之上。使大小完全相同。
  4. 对于那个观点 在视图的顶部(我们称之为view2),设置tag属性(可以 在IB)到1。
  5. 将您要连接的所有内容放在上面 view2,装饰你的细胞,无论你喜欢什么。
  6. AQGridViewController的子类中使用以下代码(当然,根据需要进行更改):
  7. `

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

    享受!

答案 3 :(得分:0)

我不熟悉AQGridView,但我相信你可以利用NSBundle的Nib加载功能。 AdvancedTableViewCells sample project的摘录说明了这个想法:

<强> 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方法的副作用,通过Nib加载机制在RootViewController上设置tmpCell属性。

答案 4 :(得分:0)

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

为了使它更快,你可以制作这个xib的uinib并重复使用它来节省磁盘i / o。

答案 5 :(得分:0)

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

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