2012-12-03 32 views
2

在我的应用程序中,我有六个按钮需要放在pscollectionview上。 我已经在应用程序中放置了pscollectionview,但我不是使用数据源和委托方法的亚伯? 任何一个能拿出这个例子都会有帮助吗? 下面是我的代码:这个问题发布后如何在iphone应用程序中实现PSCollectionView?

- (void)viewDidLoad 
{ 

    self.collectionView = [[PSCollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 


    self.collectionView.delegate = self; 
    self.collectionView.collectionViewDelegate = self; 
    self.collectionView.collectionViewDataSource = self; 
    self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
    [self.view addSubview:self.collectionView]; 


    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    btn1.frame = CGRectMake(10, 30, 140, 130); 
    [self.collectionView addSubview:btn1]; 

    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    btn2.frame = CGRectMake(170, 30, 140, 130); 
    [self.collectionView addSubview:btn2]; 

    UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    btn3.frame = CGRectMake(10, 180, 140, 130); 
    [self.collectionView addSubview:btn3]; 

    UIButton *btn4 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    btn4.frame = CGRectMake(170, 180, 140, 130); 
    [self.collectionView addSubview:btn4]; 


    UILabel *loadingLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 100, 50, 20)]; 
    loadingLabel.text = @"Loading..."; 
    loadingLabel.textAlignment = UITextAlignmentCenter; 
    [self.collectionView addSubview:loadingLabel]; 
    //self.collectionView.loadingView = loadingLabel; 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 



- (NSInteger)numberOfViewsInCollectionView:(PSCollectionView *)collectionView { 
    return 1; 
} 


- (PSCollectionViewCell *)collectionView:(PSCollectionView *)collectionView viewAtIndex:(NSInteger)index { 
} 

- (CGFloat)heightForViewAtIndex:(NSInteger)index { 
    NSDictionary *item = [self.items objectAtIndex:index]; 

    return [NDACCustomView heightForViewWithObject:item inColumnWidth:self.collectionView.colWidth]; 
} 

- (void)collectionView:(PSCollectionView *)collectionView didSelectView:(PSCollectionViewCell *)view atIndex:(NSInteger)index { 
    // NSDictionary *item = [self.items objectAtIndex:index]; 

    // You can do something when the user taps on a collectionViewCell here 
} 
+0

即时通讯设法使用这个集合视图,但我不明白什么是self.collectionView?我需要做些什么来创造这个?谢谢 – Elgert

回答

5

PSCollectionView的委托方法已经更新。 访问https://github.com/ptshih/PSCollectionView。 只是在这里分享我的代码。

- (void)viewDidLoad 
{ 
self.collectionView = [[PSCollectionView alloc] initWithFrame:self.view.frame]; 
self.collectionView.delegate = self; // This is for UIScrollViewDelegate 
self.collectionView.collectionViewDelegate = self; 
self.collectionView.collectionViewDataSource = self; 
self.collectionView.backgroundColor = [UIColor clearColor]; 
self.collectionView.autoresizingMask = ~UIViewAutoresizingNone; 

// Specify number of columns for both iPhone and iPad 
self.collectionView.numColsPortrait = 3; 
self.collectionView.numColsLandscape = 4; 

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]]; 
[self.view addSubview:_collectionView]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (Class)collectionView:(PSCollectionView *)collectionView cellClassForRowAtIndex:(NSInteger)index { 
    return [MCCollectionViewCell class]; 
} 

- (NSInteger)numberOfRowsInCollectionView:(PSCollectionView *)collectionView { 
    return 30; 
} 

- (UIView *)collectionView:(PSCollectionView *)collectionView cellForRowAtIndex:(NSInteger)index { 
    MCCollectionViewCell *cell = (MCCollectionViewCell *)[_collectionView dequeueReusableViewForClass:[MCCollectionViewCell class]]; 
    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MCCollectionViewCell" owner:self options:nil]; 
     cell = (MCCollectionViewCell *)[nib objectAtIndex:0]; 
    } 

    NSOperationQueue *imageQueue = [[NSOperationQueue alloc] init]; 
    [imageQueue addOperation:[NSBlockOperation blockOperationWithBlock:^{ 
     int r = arc4random() % 3; 
     NSString *imageName = [NSString stringWithFormat:@"image%d", r]; 
     UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"jpg"]]; 

     [[NSOperationQueue mainQueue] addOperation:[NSBlockOperation blockOperationWithBlock:^{ 
      cell.previewImageView.image = image; 
      cell.titleLabel.text = imageName; 
     }]]; 
    }]]; 

    return cell; 
} 

- (CGFloat)collectionView:(PSCollectionView *)collectionView heightForRowAtIndex:(NSInteger)index { 
    return 290.0; 
} 
+0

你知道self.collentionView是什么吗? – Elgert

+0

你必须声明一个属性,如:@property(nonatomic,strong)PSCollectionView * collectionView; – vampirewalk

+0

@vampirewalk你能提供类MCCollectionViewCell的代码吗? –

相关问题