使用UINib为UITableViewCell分配插座 [英] Assigning the outlet for a UITableViewCell using UINib

查看:48
本文介绍了使用UINib为UITableViewCell分配插座的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Apple的WWDC 2010代码中查看了TableViewUpdates/TVAnimationGestures,但在复制UITableViewCell子类时遇到了麻烦.这就是我所做的:

I looked at the TableViewUpdates/TVAnimationGestures from Apple's WWDC 2010 code and am having trouble duplicating a UITableViewCell subclass. This is what I've done:

我创建了一个具有一些简单属性的新UITableViewCell子类:

I created a new UITableViewCell subclass with some simple properties:

@interface TargetDetailTableViewCell : UITableViewCell

@property (nonatomic, retain) IBOutlet UILabel *DescriptionLabel;
@property (nonatomic, retain) IBOutlet UILabel *ValueLabel;
@property (nonatomic, retain) IBOutlet UIImageView *DotImageView;

在.m文件中,我只是释放内存.在IB中,我将我拖到IB中的UITableViewCell的类更改为TargetDetailTableViewCell.我将TargetDetailTableViewCell的插座连接到适当的标签和图像视图.

In the .m, I just release memory. In IB, I change my class to TargetDetailTableViewCell for the UITableViewCell I just dragged into IB. I connect the outlets from the TargetDetailTableViewCell to the appropriate labels and image view.

在课堂上我想使用这个:

In the class I want to use this:

@class TargetDetailTableViewCell;

//some properties
@property (nonatomic, assign) IBOutlet TargetDetailTableViewCell *TargetCell;

在.m中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    
    static NSString *TargetCellIdentifier = @"TargetDetailTableViewCellIdentifier";
    TargetDetailTableViewCell *cell = (TargetDetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:TargetCellIdentifier];

    if (cell == nil) {
        UINib *nib = [UINib nibWithNibName:@"TargetDetailTableViewCell" bundle:nil];
        [nib instantiateWithOwner:self options:nil];
        cell = self.TargetCell;
        self.TargetCell = nil;
    }
// set some labels
return cell;
}

运行它时,出现错误:Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

When I run it, I get the error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

我唯一可以看到的是Apple的示例与我的示例之间的区别是,当我在其IB中单击UITableViewCell的子类时,它们具有文件的Owner属性集.我不知道他们如何连接该插座,因为它们在使用该单元的类中被声明为属性,但是他们没有进行物理IB连接.有人可以向我解释一下还是我做错了什么?

The only think I can see that is different between Apple's example and mine is that when I ctlr-click on the subclass of UITableViewCell in their IB, they have a File's Owner property set. I have no idea how they connected that outlet as it is declared as a property in the class they use the cell, but there is no physical IB connection they make. Can someone explain that to me or what I am doing wrong?

此外,如果有人可以解释这一点,那就太好了

Also, if anyone can explain this, that would be great:

        UINib *nib = [UINib nibWithNibName:@"TargetDetailTableViewCell" bundle:nil];
        [nib instantiateWithOwner:self options:nil];
        cell = self.TargetCell;
        self.TargetCell = nil;

似乎您创建了笔尖,并且从内存中实例化的笔尖的所有者是您所在的类或自身(我的viewcontroller).然后,最后两行使我感到困惑.就像您告诉您的单元格指向新创建的对象,然后将新创建的对象设置为nil一样.我认为,该单元格现在也指向nil.谢谢.

It seems like you create the nib and the owner of the nib that gets instantiated from memory is the class you are in or self (my viewcontroller). Then the last two lines confuse me. It's like you tell your cell to point to the newly created object, then you set the newly created object to nil. Which in my head I think, the cell now points to nil as well. Thanks.

推荐答案

您需要在自定义表格视图单元格笔尖中拥有一个所有者,并且该所有者必须是您的TableViewDataSource类(即,实现cellForRowAtIndexPath方法的表格视图控制器并具有指向表格单元格的TargetCell出口).

You need to have an owner in your custom table view cell nib and that owner needs to be your TableViewDataSource class (i.e. the table view controller which implements the cellForRowAtIndexPath method and has the TargetCell outlet to the table cell).

您还需要将此TargetCell插座从文件所有者(TableViewController)连接到您的自定义表格视图.

You also need to connect this TargetCell outlet from the file owner (the TableViewController) to your custom table view.

这样做的原因是,当您以表视图控制器作为所有者加载笔尖时,它将设置您拥有的出口(TargetCell属性)以指向笔尖中定义的表视图单元格.

The reason for this is that when you load the nib, with your table view controller as the owner, it will then set the outlet that you have (the TargetCell property) to point to the table view cell defined in your nib.

然后,您将此引用复制到单元格方法变量,对其进行配置并返回它.在复制属性后将其分配给nil,因为您只需要将它作为引导程序来获取对nib中对象的引用,以便在cellForRowAtIndexPath方法中使用.

You then copy this reference to the cell method variable, configure it and return it. You assign the property to nil after copying it because you only needed it as a bootstrap to get a reference to the object in the nib for use in the cellForRowAtIndexPath method.

这篇关于使用UINib为UITableViewCell分配插座的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆