【问题标题】:Use a button on a UICollectionViewCell to display data from an array使用 UICollectionViewCell 上的按钮显示数组中的数据
【发布时间】:2013-05-03 10:09:13
【问题描述】:

我有一组NSStrings,一个UILabel 和一个UICollectionView

我的问题:

我想要数组的计数来确定有多少UICollectionViewCell

每个UICollectionViewCell 都包含一个按钮。单击后,我希望此按钮使数组中与UICollectionViewCell 的编号相对应的数据显示在标签中。

例如,如果用户点击第 13 个UICollectionViewCell 的按钮,那么数组中的第 13 个NSString 将成为UILabel 的文本。

我做了什么:

我为所有UICollectionViewCells 使用的nib 文件创建了自己的UICollectionViewCell 子类,并将按钮作为IBAction 连接到.h 文件。我还导入了MainViewController.h,它包含存储NSStrings 的数组属性。

当我在UICollectionViewCell 的操作中编辑代码时,我无法访问数组属性。该按钮确实有效 - 我在 IBAction 的方法中放置了一个 NSLog,它确实有效。

我已经搜索了数十个关于 SO 的其他答案,但没有一个回答我的具体问题。如果需要,我可以使用我的代码示例进行更新。

【问题讨论】:

  • 对这个问题有任何跟进吗?
  • 我的主要问题是我使用的单元格不是我的自定义 UICollectionViewCell 子类的实例。我使用的是常规 UICollectionViewCell 实例。菜鸟失误。感谢大家的帮助!
  • @tagabek 也许应该接受 Anupdas?

标签: ios objective-c xcode ios6 uicollectionview


【解决方案1】:

我已经为 nib 文件创建了自己的 UICollectionViewCell 子类 我用于所有 UICollectionViewCells,并连接 .h 文件的按钮作为 IBAction。

如果您将 IBAction 连接到 collectionViewCell 的子类,您需要创建一个委托以使触摸事件在您显示数据的 viewController 中可用。

一个简单的调整是将按钮添加到 collectionViewCell,将它的 IBOutlet 连接到单元格。但不是 IBAction。在cellForRowAtIndexPath: 中为包含collectionView 的viewController 中的按钮添加一个eventHandler。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //Dequeue your cell

    [cell.button addTarget:self 
                    action:@selector(collectionViewCellButtonPressed:)
          forControlEvents:UIControlEventTouchUpInside];

    return cell;

}


- (IBAction)collectionViewCellButtonPressed:(UIButton *)button{

    //Acccess the cell
    UICollectionViewCell *cell = button.superView.superView;

    NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];

    NSString *title = self.strings[indexPath.row];

    self.someLabel.text = title;

}

【讨论】:

    【解决方案2】:

    请这样尝试..

    在 YourCollectionViewCell.h 中

    为您添加到 xib 的 UIButton 创建一个 IBOutlet 而不是称为 IBAction 的按钮。请记住,您应该将插座连接到单元对象而不是 xib 中的文件所有者。

    MainViewController.m

     - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    
      cell.button.tag = indexPath.row;
      [cell.button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
    } 
    
    -(void)buttonPressed:(UIButton*)sender
    {
      NSLog(@"%d : %@",sender.tag,[array objectAtIndex:sender.tag]);
      self.textLabel.text = [array objectAtIndex:sender.tag];
     }
    

    编辑 - 处理多个部分

     -(void)buttonPressed:(UIButton*)sender
     {
      NSIndexPath  *indexPath = [self.collectionView indexPathForCell: (UICollectionViewCell *)sender.superview.superview];
    
    NSLog(@"Section : %d  Row: %d",indexPath.section,indexPath.row);
    
    if (0 == indexPath.section) {
        self.textLabel.text = [firstArray objectAtIndex:indexPath.row];
    }
    else if(1 == indexPath.section)
    {
         self.textLabel.text = [secondArray objectAtIndex:indexPath.row];
    }
    
    }
    

    【讨论】:

    • 当部分也应该使用时,你将如何处理?
    • 不可能处理多个部分。但是这里 OP 将详细信息存储在一个数组中,并获取与单元格编号相对应的适当数据。哪个实习生意味着他只有一个部分。如果他有多个部分,他不能使用数组。我看到你的答案更通用。但我认为我的代码已经足够了
    • 我实际上有 4 个部分。你能解释一下为什么我不能使用数组吗?
    • 那么你将如何识别数组中的数据?您只有单元格编号,即 indexPath.row.. 对于每个部分,其计数类似于 Sec1: 0,1,2.. Sec:0,1,2.. 。那么如何使用数组索引来识别数组中的数据属于哪个节呢?您不能使用单个数组。不同部分需要不同的数组。所以最好在你的情况下使用字典
    • @tagabek 你可以很好地使用数组。你需要这样的格式。你应该有一个字典数组。将这些词典中的每一个都视为您的部分。每个部分字典都有一个数组或行。以便于使用。
    【解决方案3】:

    当我在 UICollectionViewCell 的操作中编辑代码时,我无法访问数组属性。

    那是因为您将按钮操作连接到“错误”对象。它需要连接到 MainViewController(或任何 确实 有权访问数组属性的人)。

    你将有几个任务要执行:

    • 接收按钮操作消息。

    • 访问数组(数据的模型)。

    • 抛出一个开关,说明现在应该显示哪个单元格的标签。

    • 将集合视图告诉reloadData,从而刷新单元格。

    所有这些任务都应该最方便地属于一个对象。我假设这是 MainViewController(因此我假设 MainViewController 是集合视图的委托/数据源)。

    【讨论】:

    • 按钮操作无法连接到 MainViewController,因为它位于 UICollectionViewCell 内部。标签实际上是 MainViewController.xib 的一部分,而不是 UICollectionViewCell.nib(对不起,我应该指定)。假设我在 UICollectionView 中有 50 个 UICollectionViewCell。整个集合视图中有 50 个按钮 - 每个单元格中有一个。我想将数组的每个索引连接到每个单元格。我希望这有助于具体说明。感谢您的帮助!
    • “按钮动作无法连接到 MainViewController”。当然可以。也许您无法通过在情节提要中绘制连接来设置它,但您可以在代码中设置它(在您的collectionView:cellForItemAtIndexPath: 实现中将是一个非常方便的地方,而且现在确实有另一个答案向您展示如何做到这一点- 以这种速度,您不必为自己做任何思考)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    • 2021-10-11
    • 2021-03-19
    相关资源
    最近更新 更多