UICollectionViewCell 内的 UITableView [英] UITableView inside UICollectionViewCell

查看:22
本文介绍了UICollectionViewCell 内的 UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 UICollectionViewCell 中有 UITableView.然后我将通过 UICollectionViewCell 的子类动态更改 UITableView 的内容.

I want to have UITableView inside my UICollectionViewCell. I would then dynamically change the content of UITableView through the subclass of UICollectionViewCell.

我的 UICollectionViewCell .h 子类如下所示:

My subclass of UICollectionViewCell .h looks like this:

@interface ShopCollectionCell : UICollectionViewCell <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, weak) IBOutlet UILabel *myLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

在 .m 中只是标准的 UITableView 方法:

in .m is just standard UITableView methods:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"bla"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"bla"];
    }

    cell.textLabel.text = @"test";
    return cell;
}

在我的 UIViewController 中,我有一个 UICollectionView,我在其中执行以下操作:

in my UIViewController I have a UICollectionView where I do the following:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ShopCollectionCell *cell = (ShopCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"shopDetailCell" forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[ShopCollectionCell alloc] init];//:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.myLabel.text = @"test";

    return cell;
}

我不明白为什么我会得到僵尸对象?!

I don't understand why I am getting zombie object ?!

推荐答案

为了能够使用 dequeueReusableCellWithReuseIdentifier:forIndexPath,您还需要为该单元标识符注册一个类或一个笔尖.

To be able to use dequeueReusableCellWithReuseIdentifier:forIndexPath you need to also register a class or a nib for that cell identifier.

在您的 viewDidLoad 中,您应该使用下面两个 UITableView 方法之一来注册一个单元格

in your viewDidLoad, you should use either of the two UITableView methods below to register a cell

registerNib:forCellReuseIdentifier:
registerClass:forCellReuseIdentifier:

查看UITableView 文档 了解如何使用它们的更多细节

Check out the UITableView docs for further details on how to use them

然后您应该能够完全删除以下几行:

You should then be able to delete the following lines completely:

   if (cell == nil) {
        cell = [[ShopCollectionCell alloc] init];//:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

更新:您发布给我的错误消息让您很容易找出问题所在.您以self"作为所有者加载 NIB,然后将 self 替换为从 NIB 加载的第一个视图.我会在您的 NIB 中使用 UIView 作为 rootview,并将加载的视图添加到单元格的 contentView 属性中.

UPDATE: The error message you posted to me made it pretty easy to figure out what was wrong. You load the NIB with "self" as owner, but then you replace self with the first view loaded from the NIB. I would use a UIView as rootview in your NIB, and add that loaded view into the cell's contentView property.

这篇关于UICollectionViewCell 内的 UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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