我有一个自定义 UICollectionViewCell ,其内容也是一个集合,我想使用 UICollectionView 来显示其内容。这可能吗?我将如何实现这个目标?我制作了自定义 UICollectionViewCell 也继承自 UICollectionViewDataSource 并定义了 UICollectionView 内部属性,但事情没有按预期工作。任何想法?
以下是如何完成此操作的示例。我们有一个带有标准 UITableViewController、自定义 UITableViewCell 和单独的自由格式自定义 UIViewController 的故事板。
// ContainerCell.h
@class CellContentViewController;
@interface ContainerCell : UITableViewCell
@property (nonatomic, strong) CellContentViewController* contentViewController;
@end
// CellContentViewController.h
@interface CellContentViewController : UIViewController
@property (nonatomic, weak) IBOutlet UILabel* nameLabel;
@end
// CellContentViewController.m
@interface CellContentViewController ()
- (IBAction)didTapButton:(UIButton*)sender;
@end
@implementation CellContentViewController
- (void)didTapButton:(UIButton *)sender
{
NSLog(@"Button for %@!", self.nameLabel.text);
}
@end
// MyTableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* const kCellIdentifier = @"ContainerCell";
ContainerCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];
if (!cell.contentViewController)
{
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
CellContentViewController* contentViewController= [storyboard instantiateViewControllerWithIdentifier:@"CellContentViewController"];
cell.contentViewController = contentViewController;
[cell.contentView addSubview:cell.contentViewController.view];
}
cell.contentViewController.nameLabel.text = self.names[indexPath.row];
return cell;
}
结果是自定义视图控制器响应按钮点击。当视图在表中出现和消失时,不会调用视图生命周期方法。
我这样做是为了概念证明,但对于我正在从事的项目,我没有看到任何真正的优势CellContentViewController
是一个视图控制器而不仅仅是一个自定义视图。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)