IOS:UICollectionView UIButtons不工作

时间:2016-03-04 05:15:44

标签: ios objective-c uibutton uicollectionview uicollectionviewcell

在我的项目中,我使用UICollectionView来显示数据。数据完全显示在UICollectionViewCells中,但我也在单元格中使用“编辑”和“删除”按钮来编辑或删除数据。

当我尝试编辑任何单元格并按“编辑”按钮时,会出现问题。它仅适用于前3个单元格。当我点击第四个单元格的编辑按钮时,它在编辑页面上显示第一个单元格的结果,对于第五个单元格显示第二行的结果,对于第6个单元格显示第三行的内容,然后对于第7行,它再次显示第一行的结果细胞和这个过程一直持续到细胞结束。

编辑功能

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {

        static NSString *identifier = @"cellIdentifier";
        UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

       editbtn = (UIButton *)[cell.contentView viewWithTag:206];
        editbtn.tag = indexPath.row;
        [editbtn addTarget:self action:@selector(editprocedure:) forControlEvents:UIControlEventTouchUpInside];
        [editbtn setTintColor:[UIColor blackColor]];

        deletebtn = (UIButton *)[cell.contentView viewWithTag:207];
        [deletebtn addTarget:self action:@selector(deleteprocedure:) forControlEvents:UIControlEventTouchUpInside];
        [deletebtn setTintColor:[UIColor blackColor]];
        deletebtn.tag = indexPath.row;

        return cell;
    }

我也在模拟器上分享它的外观截图

enter image description here

以下是编辑功能的代码

-(IBAction)editprocedure:(UIButton*)sender
{
    UIButton *btnTapped = (UIButton *)sender;
    self.strdiagid = btnTapped.accessibilityIdentifier;
    NSLog(@"str ID : %@",self.strdiagid);

    self.strdiagid = [CompletephyDic objectForKey:@"id"];
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    EditProcedureVC *vch = [mainStoryboard instantiateViewControllerWithIdentifier:@"EditProcedureVC"];

    self.strdiagid = [CompletephyDic objectForKey:@"id"];
    NSString * a = self.strdiagid;
    NSLog(@"id...%@",a);

    vch.stridd = a; //stridd is the id of the procedure which is transferred to other view
    vch.infoDictionary = [dataArray objectAtIndex:sender.tag];

    [self presentViewController:vch animated:NO completion:nil];
}

4 个答案:

答案 0 :(得分:0)

试试这个:

_  _  _ 
_  _  _
_  _  _ 

答案 1 :(得分:0)

将UICollectionViewCell子类化,并将您的collectionViewCell的自定义类设置为storyboard上的此子类。创建EditButton和DeleteButton的出口。

将您的按钮标记替换为indexPath行或部分,并按以下方式访问它: -

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCollectionViewCells *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cvCells" forIndexPath:indexPath];
    [cell.editButton setTag:indexPath.row];
    [cell.button addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
    }

- (IBAction)clicked:(id)sender {

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[sender tag] inSection:0];

    CustomCollectionViewCell *cell = (CustomCollectionViewCell *) [self.collectionview cellForItemAtIndexPath:indexPath];
          //Your desired code for the specific cell
    }

希望它有所帮助。如果您有任何疑问,请告诉我。

答案 2 :(得分:0)

试试这个

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];

    [self setAutomaticallyAdjustsScrollViewInsets:NO];
}

答案 3 :(得分:0)

很遗憾,所选答案对我不起作用。这是写的内容:

我花了数小时在网上搜寻有关UIButton内部UICollectionView无法正常工作的解决方案。让我发疯,直到我终于找到了适合我的解决方案。而且我相信这也是正确的方法:破解热门测试。与解决UICollectionView Button问题相比,此解决方案可以更深入(双关语意),因为它可以帮助您将click事件传递到掩藏在阻止事件通过的其他视图下的任何按钮:

UIButton in cell in collection view not receiving touch up inside event

由于SO答案在目标C中,所以我从那里的线索中找到了快速解决方案:

http://khanlou.com/2018/09/hacking-hit-tests/

-

当我要禁用单元格上的用户交互或尝试的其他任何答案时,没有任何效果。

我上面发布的解决方案的美丽之处在于,您可以按照自己的习惯保留addTarget和选择器函数的功能,因为它们最有可能永远不会成为问题。您只需要重写一个功能即可帮助触摸事件到达目的地。

解决方案为何起作用:

在开始的几个小时里,我发现我的addTarget调用未正确记录该手势。事实证明,目标注册良好。触摸事件根本无法到达我的按钮。

现实似乎是从我读过的许多SO帖子和文章中得知,UICollectionView单元旨在容纳一个动作,由于各种原因而不是多个。因此,仅应该使用内置的选择操作。考虑到这一点,我相信绕过此限制的正确方法不是破解UICollectionView来禁用滚动或用户交互的某些方面。 UICollectionView仅在执行其工作。 正确的方法是破解点击测试,以在点击进入UICollectionView之前拦截该点击,并弄清他们正在点击哪些项目。然后,您只需将触摸事件发送到他们所点击的按钮,然后让您的普通东西完成工作即可。


我的最终解决方案(来自khanlou.com文章)是将我的addTarget声明和选择器函数放在我喜欢的任何位置(在单元类或cellForItemAt中),并在单元类中覆盖hitTest函数。

在我的细胞班里,我有:

@objc func didTapMyButton(sender:UIButton!) {
    print("Tapped it!")
}

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {

    guard isUserInteractionEnabled else { return nil }

    guard !isHidden else { return nil }

    guard alpha >= 0.01 else { return nil }

    guard self.point(inside: point, with: event) else { return nil }


    // add one of these blocks for each button in our collection view cell we want to actually work
    if self.myButton.point(inside: convert(point, to: myButton), with: event) {
        return self.myButton
    }

    return super.hitTest(point, with: event)
}

在单元格初始化中,我有:

self.myButton.addTarget(self, action: #selector(didTapMyButton), for: .touchUpInside)