GMGridview用法

    GMGridview非常强大的一个第三方开源控件,以网格的形式显示内容,支持动态添加、删除、移动单元格等,非常强大。

    一. 初始化实例

    _gmGridView = [[GMGridView alloc]initWithFrame:CGRectMake(0, 106, width, height)];
    //_gmGridView.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
    _gmGridView.backgroundColor = View_BackgroundColor;
    [self.view addSubview:_gmGridView];
    [self.view sendSubviewToBack:_gmGridView];
    _gmGridView.scrollEnabled = YES;
    [_gmGridView setAlwaysBounceVertical:YES];
    _gmGridView.clipsToBounds = YES;
    _gmGridView.showsVerticalScrollIndicator = NO;
    _gmGridView.style= GMGridViewStylePush;
    
    _gmGridView.itemSpacing = spacing;

    _gmGridView.minEdgeInsets = UIEdgeInsetsMake(8.75, 8.75, 8.75, 8.75);
    //_gmGridView.centerGrid = NO;
    _gmGridView.actionDelegate = self;//
    _gmGridView.sortingDelegate = self;//
    _gmGridView.dataSource = self;//数据源

    二. 加载数据

    

    [_gmGridView reloadData];

    三. 生成单元格

    为了提升性能,这里不直接使用控件,而是通过添加layer的形式来为单元格添加内容。

-(GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index
{
    CGSize size = [self GMGridView:gridView sizeForItemsInInterfaceOrientation:[[UIApplication sharedApplication]statusBarOrientation]];
    GMGridViewCell *cell=[gridView dequeueReusableCell];
    if (!cell)
    {
        cell = [[GMGridViewCell alloc]init];
        cell.selectButtonIcon = [UIImage imageNamed:@"zq_house_setting_skinEditNoSelect.png"];
        cell.selectButtonOffset = CGPointMake(0, 0);
        
        cell.selectButton.frame = CGRectMake(cell.selectButton.frame.origin.x,
                                             cell.selectButton.frame.origin.y,
                                             95,
                                             95);
        [cell.selectButton setImage:[UIImage imageNamed:@"zq_house_setting_skinEditSelect.png"] forState:UIControlStateSelected];
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(23, 23, size.width, size.height)];
        view.backgroundColor = Scene_Normal_BackgroundColor
        cell.contentView = view;
    
        // 设置不能同时选中
        [cell.contentView setExclusiveTouch:YES];
        CALayer *imgLayer = [[CALayer alloc]init];
        imgLayer.frame = CGRectMake(5, 5, 84, 84);
        imgLayer.name = @"imgLayer";
        [cell.contentView.layer addSublayer:imgLayer];
        
    }

    CALayer *imgLayer =[ViewUtil getSubLayer:@"imgLayer" ofLayer:cell.contentView.layer];
    RoomModel *room = [_currentData objectAtIndex:index];
    NSString *backgroundImageName =room.img;
    
    UIImage *img = [UIImage imageNamed:backgroundImageName];
    imgFrameLayer.contents = (id)img.CGImage;
   
    

    
    return cell;
}

//单元格数量
-(NSInteger )numberOfItemsInGMGridView:(GMGridView *)gridView
{
    return [_currentData count];
}
//单元格大小
-(CGSize)GMGridView:(GMGridView *)gridView sizeForItemsInInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    
    if (1)
    {
        if (UIInterfaceOrientationIsLandscape(orientation))
        {
            return CGSizeMake(95, 116);//zhy   设置view的大小
        }
        else
        {
            return CGSizeMake(95, 116);
        }
    }
}

    四. 点击事件

- (void)GMGridView:(GMGridView *)gridView didTapOnItemAtIndex:(NSInteger)position
{
    // do something    
}

   五. 动态添加删除单元格

    当数据源发生变化时,我们并不需要全部重新加载数据,只需要刷新变化的数据即可,如:

       [_currentData insertObject:_addedRoom atIndex:[_currentData count]-1];//往数据源中添加数据
       [_gmGridView insertObjectAtIndex:[_currentData count]-2 withAnimation:GMGridViewItemAnimationFade];//以动画的形式添加单元格


    同理,当删除数据时:

 

[_gmGridView removeObjectAtIndex:i withAnimation:GMGridViewItemAnimationFade];

     更新数据时:

[_gmGridView reloadObjectAtIndex:i withAnimation:GMGridViewItemAnimationFade];

    六. 移动单元格


//移动中的单元格是否抖动
-(BOOL)GMGridView:(GMGridView *)gridView shouldAllowShakingBehaviorWhenMovingCell:(GMGridViewCell *)view atIndex:(NSInteger)index
{
    return YES;
}

//移动到某个地方然后插入
-(void)GMGridView:(GMGridView *)gridView moveItemAtIndex:(NSInteger)oldIndex toIndex:(NSInteger)newIndex
{
    NSInteger newOrder = ((RoomModel *)[_currentData objectAtIndex:newIndex]).order+1;
    RoomModel *object = [_currentData objectAtIndex:oldIndex];
    [_currentData removeObject:object];
    [_currentData insertObject:object atIndex:newIndex];
}



这里只介绍了gmgridview的主要特性。






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiegwei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值