iOS-UICCollection Cell、UITable Viewellビューを単独で使用する

3191 ワード

title:iOS-UICCollection Celview単独で使用するビューdate:2026-06-30 16:15 categories:
  • Code
  • iOS tags:
  • UICCollection Cell
  • collection Cellビューを単独で使う
    偶然他の人の工事の中で見たのですが、方法は簡単です.collection Cellを使ってviewに相当するビューを作成するという簡単な説明をさせていただきます.
    使い方
    #define SCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width
    #define SCREEN_HEIGHT  [UIScreen mainScreen].bounds.size.height
    #define floatByScreenWidth(a) ((a)/320.0)*SCREEN_WIDTH
    
    1.カスタムコレクションCellがあなたに必要な属性を追加します.ここではもう言わないです.
    2.一つのビューをカスタマイズして一つの方法を宣言する
    - (void)setView; //       
    
    - (void)setView { //     
        for (int i = 0;  i < 8; i++) { //         
            //   cell                 
                MyCollectionCell *cell = [[[NSBundle mainBundle]loadNibNamed:@"MyCollectionCell" owner:self options:nil]lastObject];
                cell.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:arc4random()%255/255.0];
                cell.nametitle.text = [NSString stringWithFormat:@" %dcell",i];
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
                [tap addTarget:self action:@selector(ClickVideoSites:)];
                cell.tag = i+1;
                tap.view.tag = i+1;
                [cell addGestureRecognizer:tap];
                
                int x = i/8;
                if (x<4) {
                    cell.frame = [self setFrameBynum:i andPage:x];
                }else{
                    cell.frame = [self setFrameBynum:i andPage:x];
                }
                [self addSubview:cell];  // self      view  
            }
        }
     }
     
     //  cell   
    - (CGRect)setFrameBynum:(int)num andPage:(int)page
        {
            CGFloat length = floatByScreenWidth(74);
            CGFloat height = length/74*85;
            CGFloat width = (SCREEN_WIDTH-4*length)/5;
            
            if (num  <  4) {
                return CGRectMake(width + page * SCREEN_WIDTH + (length+width)*num, 0, length, height);
            }
            else
                return CGRectMake(width + page * SCREEN_WIDTH + (length+width)*(num-4),height + 10, length, height);
        }
    
    
    3.コントロラーで使用する
        MyView * _myView = [[MyView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [_myView setView];
        [self.view addSubview:_myView];
    
    
    
    class XYNoDataView: UIView {
        
        var nodataView: XYAddressMyFriendNoDataCell?
        private func setup() {
            
            self.backgroundColor = #colorLiteral(red: 0.9490196078, green: 0.9607843137, blue: 0.968627451, alpha: 1)
            let rect = CGRect(x: 0, y: 68, w: UIScreen.main.bounds.size.width, h: 240)
            let nodataView = Bundle.main.loadNibNamed("XYAddressMyFriendNoDataCell", owner: nil, options: nil)?.first as! XYAddressMyFriendNoDataCell
            nodataView.frame = rect
            self.addSubview(nodataView)
        }
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            setup()
        }
        
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            setup()
        }
    }
    
    
    
    ここに来てもうユーザー定義のcollection cellビューの使用を実現しました.助けてあげたいです.ありがとうございます.