Adding UIButton in each UICollectionViewCell in Scrolling Filmstrip within UITableView

天涯浪子 提交于 2019-12-11 12:46:36

问题


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:

  • 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.

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

My question is that I can't get my UIButton at least to show up (for now) with this below code:

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.


回答1:


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



来源:https://stackoverflow.com/questions/31792650/adding-uibutton-in-each-uicollectionviewcell-in-scrolling-filmstrip-within-uitab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!