【问题标题】:Custom UITableViewCell Not Loading自定义 UITableViewCell 未加载
【发布时间】:2012-11-25 11:36:55
【问题描述】:

我正在使用界面生成器创建自己的自定义 UITableViewCell。我支持 iOS 5 和 iOS 6,但我不想使用 Storyboard。请不要建议情节提要。我坚持使用 Interface Builder 并以编程方式编写。

我创建了一个继承 UITableViewCell 的类。这是 .h 文件:

@interface CategoryCell : UITableViewCell
{
    __weak IBOutlet UIImageView *image;
    __weak IBOutlet UILabel *name;
    __weak IBOutlet UILabel *distance;
    __weak IBOutlet UILabel *number;
    __weak IBOutlet UIImageView *rating;
}

@property (nonatomic, weak) IBOutlet UIImageView *image;
@property (nonatomic, weak) IBOutlet UILabel *name;
@property (nonatomic, weak) IBOutlet UILabel *distance;
@property (nonatomic, weak) IBOutlet UILabel *number;
@property (nonatomic, weak) IBOutlet UIImageView *rating;

@end

XIB 文件是 UIViewController 类型,并有一个 CategoryCell 类型的视图。我根据需要连接了插座。

问题

dequeueResuableCellWithIdentifier 没有调用自定义单元格。这是我所拥有的:

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

        if (cell == nil) {

            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CategoryCell" owner:self options:nil];
            cell = [topLevelObjects objectAtIndex:0];
            .....
        }
        return cell
}

当我将loadBundle 行替换为:cell = [[CategoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 时,它确实有效。但是笔尖没有加载。所以加载了一个单元格,但不是我自己的单元格,所以我无法设置我想要的标签和图像。添加常规加载捆绑行(如上面的示例所示)并在自定义单元格的init 方法上设置断点时,它不会被调用。此外,我得到的是一个全白屏幕,它覆盖了模拟器中的整个 iPhone 屏幕。

为什么会这样?我在这里做错了什么?当我尝试将网点设置为strong(我知道我不应该这样做)时,它也不起作用。

编辑

我通过将 NSBundle 行替换为:

UIViewController *temporaryController = [[UIViewController alloc] initWithNibName:@"CategoryCell" bundle:nil];
cell = (CategoryCell *)temporaryController.view;

我对 NSBundle 方法做错了什么?据说这应该是“更简单”的方法。

【问题讨论】:

  • 您是否设置了断点并查看了 topLevelObjects 的值以及单元格的值?这将是有用的信息,看看它们是否为零。

标签: iphone objective-c ios uitableview


【解决方案1】:

你试过tableview中的registerNib方法吗?从 iOS 5 开始加载 nib 非常方便。

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"CategoryCell" bundle:nil]
         forCellReuseIdentifier:@"CategoryCell"];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"CategoryCell";
    CategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    return cell
}

确保您在 CategoryCell.nib 中定义了标识符,它位于属性检查器下!希望这项工作。

【讨论】:

  • 你检查过 nib 中的类是指向 CategoryCell 吗?不是文件的所有者,而是 nib 中 Objects 下的单元格。
  • 是的,我做到了,它是 CategoryCell 类型
  • 并且这个xib的文件所有者应该是NSObject而不是UIViewController,因为如果你以这种方式实现,你不需要将nib附加到控制器。
【解决方案2】:

XIB 文件是 UIViewController 类型,并有一个 CategoryCell 类型的视图。我按需要连接了插座。

这里的问题是您的 XIB 的根对象需要是 UITableViewCell 或其子类。
并确保在您的 XIB 中设置了您的重用标识符。

【讨论】:

  • 它是 CategoryCell 类型。 File 的 Owner 是 UIViewController 但 View 是 CategoryCell 类型,它是 UITableViewCell 的子类。我还设置了重用标识符。
猜你喜欢
  • 1970-01-01
  • 2014-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-11
  • 1970-01-01
相关资源
最近更新 更多