GMGridView用法小试
GMGridView继承于UIScrollView,很强大的一款控件,今天小试了一下。对于该控件不支持BUTTON点击事件做了处理,有什么写的不好的请大家提出建议。
#import <UIKit/UIKit.h>
#import "GMGridView.h"
@interface WMGMViewController : UIViewController<GMGridViewDataSource>
{
__gm_weak GMGridView *_gmGridView;
GMGridView *gmGridView;
}
@end
#import "WMGMViewController.h"
#import "GMGridViewLayoutStrategies.h"
@interfaceWMGMViewController ()
@end
@implementation WMGMViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
returnself;
}
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view.
[selfinitGMGridView];
}
-(void)initGMGridView
{
gmGridView = [[GMGridViewalloc] initWithFrame:CGRectMake(0, 0, 320,480)];
gmGridView.style = GMGridViewStylePush;
//间距
gmGridView.itemSpacing = 30;
//离边界位置
gmGridView.minEdgeInsets = UIEdgeInsetsMake(5,32.5,5,5);
//是否设置在屏幕中心
gmGridView.centerGrid = NO;
gmGridView.scrollEnabled=YES;
gmGridView.dataSource=self;
gmGridView.bounces=NO;
gmGridView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
gmGridView.layoutStrategy = [GMGridViewLayoutStrategyFactorystrategyFromType:GMGridViewLayoutVertical];
[self.viewaddSubview:gmGridView];
_gmGridView = gmGridView;
}
-(void)btnClicked:(UIButton *)sender
{
NSLog(@"btn clicked");
}
#pragma mark - GMGridViewDataSourceDelegate method
- (CGSize)GMGridView:(GMGridView *)gridView sizeForItemsInInterfaceOrientation:(UIInterfaceOrientation)orientation
{
returnCGSizeMake(200/4,80);
}
- (NSInteger)numberOfItemsInGMGridView:(GMGridView *)gridView
{
return40;
}
//GMGridView不支持button点击事件
//GMGridView.m 添加方法支持button点击
//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
- (GMGridViewCell *)GMGridView:(GMGridView *)gridView cellForItemAtIndex:(NSInteger)index
{
GMGridViewCell *cell = [gridView dequeueReusableCell];
cell = [[GMGridViewCellalloc] init];
UIButton *btn=[UIButtonbuttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(0, 0,40,40);
btn.backgroundColor=[UIColorblueColor];
[btn addTarget:selfaction:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
UILabel *label=[[UILabelalloc]initWithFrame:CGRectMake(0,40+5,40,20)];
label.text=@"标签";
label.textColor=[UIColorblackColor];
label.backgroundColor=[UIColorclearColor];
[cell addSubview:label];
return cell;
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
下载地址:http://down.51cto.com/data/912351
转载于:https://blog.51cto.com/ios2013yoyo/1269493