集合视图需要建议

时间:2012-11-21 18:53:20

标签: objective-c ios xcode uicollectionview uicollectionviewcell

我实现了一个基于cocoa控件项目的集合视图

http://www.cocoacontrols.com/platforms/ios/controls/klsectionselect

我有6个项目存储在一个数组中。

我的问题是:使用if语句,如何引用存储在P列表中的数组中的项目1到6,以便我可以在选择时为这些项目添加实现/操作?

到目前为止,我尝试查看容器视图的Apple文档 - 实际上没有任何进一步的转发

我还尝试使用if ((collectionView = 0))和其他此类猜测添加if语句'

enter image description here

-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.sectionData count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
[collectionView registerClass:[KLHeaderViewCell class] forCellWithReuseIdentifier:@"Cell"];
KLHeaderViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
NSDictionary* cellDictionary = [self.sectionData objectAtIndex:indexPath.row];
[cell.image setImage:[UIImage imageNamed: [cellDictionary objectForKey:@"image"]]];
[cell.label setText:[cellDictionary objectForKey:@"text"]];
return cell;
}


-(void) didSelectItem:(UICollectionView*)collectionView item:(UICollectionViewCell*) cell {


        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Note1" message:@"© 2012 " delegate:nil cancelButtonTitle: @"Ok" otherButtonTitles: nil];

    [alert show];



}

这是我试过的,可能有助于解释

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

if 
    ((collectionView = Item 0)){

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Note1" message:@"© 2012 " delegate:nil cancelButtonTitle: @"Ok" otherButtonTitles: nil];

    [alert show];

    }

else if ((collectionView =Item 1)){

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"New Note" message:@" © 2012 " delegate:nil cancelButtonTitle: @"Ok" otherButtonTitles: nil];

    [alert show];




}

}

3 个答案:

答案 0 :(得分:0)

而不是这样做:

-(void) didSelectItem:(UICo`enter code here`llectionView*)collectionView item:(UICollectionViewCell*) cell {

......你应该写:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

答案 1 :(得分:0)

实现委托方法, - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath。

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
 NSDictionary* cellDictionary = [self.sectionData objectAtIndex:indexPath.row];
} 

确保将类设置为集合视图的委托。

答案 2 :(得分:0)

我不确定我是否理解您的问题,但您可能会尝试查看所选项目行。

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath (NSIndexPath *)indexPath {

if (indexPath.row = 0){

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Note1" 
                                                    message:@"© 2012 " 
                                                   delegate:nil 
                                          cancelButtonTitle: @"Ok" 
                                          otherButtonTitles: nil];
    [alert show];
}
else if (indexPath.row = 1){

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"New Note" 
                                                    message:@"© 2012 " 
                                                   delegate:nil 
                                          cancelButtonTitle: @"Ok" 
                                          otherButtonTitles: nil];
    [alert show];
}
else if ...

}