在 UITableView 中的滚动胶片中的每个 UICollectionViewCell 中添加 UIButton
Adding UIButton in each UICollectionViewCell in Scrolling Filmstrip within UITableView
首先,让我描述一下目前的情景。以下是我的 UICollectionView 在 UITableView 中的滚动胶片样式的基本原理:
- 创建带有自定义 UITableViewCell
的普通 UITableView
- 创建一个自定义 UIView,它将添加到单元格的 contentView
- 自定义 UIView 将包含一个 UICollectionView
- 自定义 UIView 将成为 UICollectionView 的数据源和委托,并管理 UICollectionView 的流布局
- 使用自定义 UICollectionViewCell 来处理集合视图数据
- 使用 NSNotification 通知主控制器的 UITableView 何时选择了集合视图单元格并加载详细视图。
到目前为止,我已经能够创建上述内容,但正如您在图片中看到的那样,我还想在每个 UICollectionViewCell 中添加一个 UIButton,这样当我点击 UIButton 它的图像将变为选中标记,并且 UICollectionViewCell 中的数据将保存到一个数组中。最后,当我点击右上角的三角形按钮时,它会推送到另一个视图,其中包含保存所选数据的数组。
这是我得到的所有相关 类:
UITableView
- ViewController.h
- ViewController.m
UIView
- ContainerCellView.h
- ContainerCellView.m
UICollectionViewCell
- CollectionViewCell.h
- CollectionViewCell.m
UITableViewCell
- ContainerCell.h
- ContainerCell.m
我的问题是我无法让我的 UIButton 至少显示(目前) 使用以下代码:
ContainerCellView.m
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
NSDictionary *cellData = [self.collectionData objectAtIndex:[indexPath row]];
...
// >>> Select Button <<<
UIButton *button = (UIButton *)[cell viewWithTag:200];
[button setFrame:CGRectMake(0, 0, 50, 60)];
[button setTitle:@"Button" forState:UIControlStateNormal];
[cell.contentView addSubview:button]; //???
// >>> End Select Button <<<<
...
return cell;
}
ContainerCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
_collectionView = [[NSBundle mainBundle] loadNibNamed:@"ContainerCellView" owner:self options:nil][0];
_collectionView.frame = self.bounds;
[self.contentView addSubview:_collectionView];
}
return self;
}
我做错了什么,我应该如何做得更好?如果您需要更多信息,我们非常欢迎您提出要求。提前致谢。
确保 (UIButton *)[cell viewWithTag:200]
返回您所期望的,此处的新按钮将满足您的目的。
首先,让我描述一下目前的情景。以下是我的 UICollectionView 在 UITableView 中的滚动胶片样式的基本原理:
- 创建带有自定义 UITableViewCell 的普通 UITableView
- 创建一个自定义 UIView,它将添加到单元格的 contentView
- 自定义 UIView 将包含一个 UICollectionView
- 自定义 UIView 将成为 UICollectionView 的数据源和委托,并管理 UICollectionView 的流布局
- 使用自定义 UICollectionViewCell 来处理集合视图数据
- 使用 NSNotification 通知主控制器的 UITableView 何时选择了集合视图单元格并加载详细视图。
到目前为止,我已经能够创建上述内容,但正如您在图片中看到的那样,我还想在每个 UICollectionViewCell 中添加一个 UIButton,这样当我点击 UIButton 它的图像将变为选中标记,并且 UICollectionViewCell 中的数据将保存到一个数组中。最后,当我点击右上角的三角形按钮时,它会推送到另一个视图,其中包含保存所选数据的数组。
这是我得到的所有相关 类:
UITableView
- ViewController.h
- ViewController.m
UIView
- ContainerCellView.h
- ContainerCellView.m
UICollectionViewCell
- CollectionViewCell.h
- CollectionViewCell.m
UITableViewCell
- ContainerCell.h
- ContainerCell.m
我的问题是我无法让我的 UIButton 至少显示(目前) 使用以下代码:
ContainerCellView.m
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
NSDictionary *cellData = [self.collectionData objectAtIndex:[indexPath row]];
...
// >>> Select Button <<<
UIButton *button = (UIButton *)[cell viewWithTag:200];
[button setFrame:CGRectMake(0, 0, 50, 60)];
[button setTitle:@"Button" forState:UIControlStateNormal];
[cell.contentView addSubview:button]; //???
// >>> End Select Button <<<<
...
return cell;
}
ContainerCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
_collectionView = [[NSBundle mainBundle] loadNibNamed:@"ContainerCellView" owner:self options:nil][0];
_collectionView.frame = self.bounds;
[self.contentView addSubview:_collectionView];
}
return self;
}
我做错了什么,我应该如何做得更好?如果您需要更多信息,我们非常欢迎您提出要求。提前致谢。
确保 (UIButton *)[cell viewWithTag:200]
返回您所期望的,此处的新按钮将满足您的目的。