首页 文章

当按钮是UICollectionViewCell的子视图时,未调用UIButton的目标方法

提问于
浏览
1

我在UICollectionViewCell中有一个UIButton . UIButton有一个内部触摸事件的目标 . 如何确保在点击按钮而不是UICollectionView委托方法时调用此目标方法?

4 回答

  • 0

    对于UICollection视图我使用此代码根据您的要求修改它

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
     {
        static NSString *identifier = @"cellIdentifier";
        UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    
    
        for (UIView * subView  in cell.contentView.subviews)
        {
            if ([subView isKindOfClass:[UIButton class]])
            {
                yourButton = (UIButton *)[cell viewWithTag:103];
            }
        }
    
        [yourButton addTarget:self action:@selector(AddFriends:) forControlEvents:UIControlEventTouchUpInside];
        [yourButton setTintColor:[UIColor blackColor]];
    
        return cell;
     }
    
    
     -(void)AddFriends:(id)sender
     {
        [self showLoadingView];
        UIButton *btnTapped = (UIButton *)sender;
        NSString* detailsId = btnTapped.accessibilityIdentifier;
        NSLog(@"str ID : %@",detailsId);
        .
        .
        .
        .
     }
    
  • 0

    请试试这段代码 .

    在viewDidLoad方法中,这里CollectionCell是我创建的自定义单元格 .

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.register(CollectionCell.self, forCellWithReuseIdentifier: "CollectionCell")
    }
    

    CollectionView DataSource方法,

    extension ViewController: UICollectionViewDataSource {
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! CollectionCell
    
            let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
            btn.tag = indexPath.row
            btn.setTitle("Tap Me", for: .normal)
            btn.setTitleColor(UIColor.blue, for: .normal)
            btn.backgroundColor = UIColor.lightGray
            btn.addTarget(self, action:#selector(btnTapped(sender:)), for: .touchUpInside)
            cell.addSubview(btn)
    
            return cell
        }
    
        func numberOfSections(in collectionView: UICollectionView) -> Int {
            return 1
        }
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 30
        }
    }
    

    和UICollectionView Delegate方法,

    extension ViewController: UICollectionViewDelegate {
        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            print("Did Select")
        }
    }
    

    你的按钮点击方法是这样的,

    @objc func btnTapped(sender: UIButton) {
        print("Button tapped at index ", sender.tag)
    }
    

    这对我来说可以 . 请根据您的要求进行必要的更改 .

    如有任何疑问,请告诉我,以便我们进一步帮助您 .

  • 0

    您可以轻松找到调用的事件 . 如果单击 collectionView cell ,则调用 collectView delegate 方法

    SWIFT

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            print("collection did select")
        }
    

    如果你点击单元格的按钮

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! CollectionCell
    
            cell.YOUR_BUTTON.tag = indexPath.item
            cell.YOUR_BUTTON.addTarget(self, action:#selector(btnClick(sender:)), for: .touchUpInside)
    
            return cell
        }
    
    @objc func btnClick(sender: UIButton) {
        print(sender.tag)
    }
    

    OBJECTIVE C

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell" forIndexPath:indexPath];
    
        cell.YOUR_BUTTON.tag = indexPath.item
        [cell.YOUR_BUTTON addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    }
    -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath  {
        NSLog(@"collection did select");
    }
    
    -(void)btnClick(sender: UIButton) {
        NSLog(@"button click index %d",sender.tag)
    }
    
  • 0

    上面的所有答案都是正确的并且会给你你想要的结果,但是我建议你为单元类制定自己的协议并实现委托方法来获得结果,而不是使用tag和all .

    我也在另一篇文章中回答了这个问题 . 请检查链接..并停止分配标签并使用它来识别您的按钮并实施按钮单击

    https://stackoverflow.com/a/48882197/9130596

相关问题