ios - UICollectionViewCell 中的 UITableView

标签 ios objective-c uicollectionview uicollectionviewcell

我想在我的 UICollectionViewCell 中包含 UITableView。然后我会通过 UICollectionViewCell 的子类动态更改 UITableView 的内容。

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

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

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

- (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,我在其中执行以下操作:

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

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

enter image description here

最佳答案

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

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

registerNib:forCellReuseIdentifier:
registerClass:forCellReuseIdentifier:

查看 UITableView docs有关如何使用它们的更多详细信息

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

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

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

关于ios - UICollectionViewCell 中的 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22485703/

相关文章:

ios - 将观察者设置为对象更改

objective-c - Xcode需要Openssl RSA解密功能

ios - UICollectionView 单元格忽略 UICollectionViewDelegateFlowLayout - sizeForItemAt

ios - 在 UIStackview 中添加时未在 UICollectionView 中触发方法

ios - Xcode 7 在文件中缺少所需的体系结构 arm64

ios - 在 iOS 8+ 中使用什么字体显示这些 un​​icode 字符

ios - 禁用特定 UIView iOS 12 的自动旋转

objective-c - LLVM 在编译前会做简单的算术运算吗?

iphone - 字符串中的通配符

ios - 自定义 UICollectionViewLayout 不同单元格大小