Variable width & height of UICollectionViewCell using AutoLayout with Storyboard (without XIB)

后端 未结 3 2325
长情又很酷
长情又很酷 2021-02-19 19:05

Here is an design issue in the app that uses AutoLayout, UICollectionView and UICollectionViewCell that has automatically resizable width & height depending on AutoLayout co

3条回答
  •  太阳男子
    2021-02-19 20:03

    Just like when dynamically size Table View Cell height using Auto Layout you don't need call

    func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

    in

    optional func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat

    the proper way is create a local static cell for height calculation like a class method of the custom cell

    + (CGFloat)cellHeightForContent:(id)content tableView:(UITableView *)tableView {
        static CustomCell *cell;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            cell = [tableView dequeueReusableCellWithIdentifier:[CustomCell cellIdentifier]];
        });
    
        Item *item = (Item *)content;
        configureBasicCell(cell, item);
    
        [cell setNeedsLayout];
        [cell layoutIfNeeded];
        CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
        return size.height + 1.0f; // Add 1.0f for the cell separator height
    }
    

    and I seriously doubt storyboard is some industry standard. xib is the best way to create custom cell like view including tableViewCell and CollectionViewCell

提交回复
热议问题