在 UITableView 中的滚动幻灯片中的每个 UICollectionViewCell 中添加 UIButton [英] Adding UIButton in each UICollectionViewCell in Scrolling Filmstrip within UITableView

查看:22
本文介绍了在 UITableView 中的滚动幻灯片中的每个 UICollectionViewCell 中添加 UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,让我描述一下我目前的情况.以下是 UITableView 中滚动幻灯片样式的 UICollectionView 如何工作的基本概要:

First off, let me describe the scenario I've got so far. Here's the basic rundown of how my UICollectionView in Scrolling Filmstrip style within UITableView works:

  • 使用自定义的 UITableViewCell
  • 创建一个普通的 UITableView
  • 创建一个自定义的UIView,将添加到单元格的contentView
  • 自定义 UIView 将包含一个 UICollectionView
  • 自定义UIView 将成为UICollectionView 的数据源和委托,并管理UICollectionView
  • 的流布局
  • 使用自定义 UICollectionViewCell 来处理集合视图数据
  • 使用 NSNotification 通知主控制器的 UITableView 在选择了集合视图单元格并加载详细视图时.
  • Create a normal UITableView with a custom UITableViewCell
  • Create a custom UIView that will be added to the cell's contentView
  • The custom UIView will contain a UICollectionView
  • The custom UIView will be the datasource and delegate for the UICollectionView and manage the flow layout of the UICollectionView
  • Use a custom UICollectionViewCell to handle the collection view data
  • Use NSNotification to notify the master controller's UITableView when a collection view cell has been selected and load the detail view.

到目前为止,我已经能够创建上面描述的那些,但是正如你在图片中看到的,我还想在每个 UICollectionViewCell 中添加一个 UIButton,这样当我点击 UIButton 时,它的图像将更改为选中标记,并且 UICollectionViewCell 中的数据将保存到数组中.最后,当我点击右上角的三角形按钮时,它将推送到另一个视图,其中包含保存所选数据的数组.

So far, I've been able to create those described above but as you can see in the picture I also want to add an UIButton in each UICollectionViewCell so that when I tap the UIButton its image will change to checked mark and data in that UICollectionViewCell will be saved into an array. At the end, when I tap the top-right triangle button it will push to another view with the array that saves selected data passed on.

这是我拥有的所有相关课程:

Here're all the relevant classes that I've got:

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

我做错了什么,我应该如何做得更好?如果您需要更多信息,欢迎您提出要求.提前致谢.

What have I done wrong and how should I do this better? If you need more info, you're more than welcome to request. Thanks in advance.

推荐答案

确保 (UIButton *)[cell viewWithTag:200] 正在返回您期望的内容,这里的新按钮将满足您的目的.

Make sure (UIButton *)[cell viewWithTag:200] is returning what you expect, a new button here would serve your purpose.

这篇关于在 UITableView 中的滚动幻灯片中的每个 UICollectionViewCell 中添加 UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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