【问题标题】:refresh the action of a IBAction刷新 IBAction 的动作
【发布时间】:2015-04-14 11:40:23
【问题描述】:

我有一个UIButton 可以将我的UICollectionView 滚动到右侧。该按钮被添加到我的UICollectionView 的超级视图中,因此它漂浮在单元格的顶部。 但我只能让UIAction 发生一次。如何复制操作或刷新按钮以便我可以再次使用它。 这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(910, 345 , 100, 100);
    button.backgroundColor = [UIColor purpleColor];
    [self.collectionView.superview addSubview:button];
    [button addTarget:self action:@selector(changeContentOffset:) forControlEvents:UIControlEventTouchUpInside];
}

- (IBAction)changeContentOffset:(id)sender {
    [self.collectionView setContentOffset:CGPointMake(910, -65) animated:YES];
}

【问题讨论】:

  • 每次你点击它时都会调用按钮操作。无论如何,在将按钮作为子视图添加到 collectionView 之前添加按钮操作。你从 collectionview 中删除按钮的任何地方?
  • 将所有按钮代码放在 cellForItemAtIndexPath 而不是 ViewDidLoad 中,并将按钮添加到 contentView 而不是 collectionView.superview..

标签: ios objective-c uibutton uicollectionview refresh


【解决方案1】:

可以在self.view中添加按钮

在你的- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

您可以简单地将按钮添加到单元格内容视图的方法[cell.contentView addSubview: button];

【讨论】:

  • 它仍然只会滚动一次。我的按钮正在工作,但它只滚动一次
【解决方案2】:

如果您在 IBAction 内的行中设置断点,您是否只停止过一次?根据您的问题,您希望每次单击该按钮时都向右滚动,但您的方法不会这样做。

该方法在您第一次点击按钮时设置UICollectionViewcontentOffset,但在其他时候,由于UICollectionView 已经移动,您看不到任何变化。尝试将您的方法替换为:

- (IBAction)changeContentOffset:(id)sender { 
    CGPoint offset = self.collectionView;
    offset.x += 910;
    offset.y -= 65;
    [self.collectionView setContentOffset:offset animated:YES];
}

【讨论】:

    【解决方案3】:

    你必须改变这个方法来依赖collectionView的实际位置...

     self.collectionView.paginateEnabled = YES;
    
    
    - (IBAction)changeContentOffset:(id)sender {
       float x = cellWidth + self.collectionView.contentOffset.x;
       [self.collectionView setContentOffset:CGPointMake(910, x) animated:YES];
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-29
      • 2022-07-22
      • 1970-01-01
      相关资源
      最近更新 更多