2012-12-03 4 views
2

내 응용 프로그램에서 나는 pscollectionview에 있어야 할 여섯 개의 버튼이 있습니다. 나는 app에 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 클래스의 코드를 제공 할 수 있습니까? –

관련 문제