Adding UIButton in each UICollectionViewCell in Sc

2019-08-11 16:08发布

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.

enter image description here

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条回答
萌系小妹纸
2楼-- · 2019-08-11 16:56

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

查看更多
登录 后发表回答