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的主要特性。