【问题标题】:Advice needed on collection view收藏视图需要的建议
【发布时间】:2012-11-21 18:53:20
【问题描述】:

我正在实现一个基于可可控件项目的集合视图

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

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

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

到目前为止,我尝试查看有关容器视图的苹果文档 - 并没有进一步推进

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

-(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];




}

}

【问题讨论】:

  • 您的问题不清楚。您可以像在 cellForItemAtIndexPath: 方法中那样引用项目。您是在问如何访问所选的?
  • 抱歉,如果我没有正确解释这一点。我有六个对象,但我只有一个实现(用于警报视图)如何选择项目 2、项目 3 等if selected。

标签: objective-c ios xcode uicollectionview uicollectionviewcell


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    - (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 ...
    

    }

    【讨论】:

      【解决方案3】:

      不要这样做:

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

      ...你应该写:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-23
        • 2017-12-25
        • 1970-01-01
        • 2011-08-23
        • 1970-01-01
        • 2015-06-11
        • 2011-11-26
        • 1970-01-01
        相关资源
        最近更新 更多