我想在我的 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;
}
我不明白为什么我会得到僵尸对象?!
最佳答案
为了能够使用 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/