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

By simon at 2018-02-28 • 0人收藏 • 44人看过

我在UICollectionViewCell中有一个UIButton。 UIButton有一个触摸的目标 内部事件。我如何确保这个目标m方法在水龙头上被调用 按钮,而不是集合视图委托方法?

4 个回复 | 最后更新于 2018-02-28
2018-02-28   #1

请试试这个代码。 在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委托方法,
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)
}
这对我来说可以。请根据您的要求做必要的更改。 如有任何疑问,我可以告诉我帮助你进一步。

2018-02-28   #2

您可以轻松找到要调用的事件。如果你点击collectionView cell那么 collectView delegate方法叫哪个 迅速

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)
}
*目标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)
}

2018-02-28   #3

对于UICollectionview我使用这段代码根据你的修改 需求

 - (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);
    .
    .
    .
    .
 }

2018-02-28   #4

以上所有答案都是正确的,并会给你你想要的结果,但我 建议你为cel制作自己的协议升级并实施 委托方法来获得结果,而不是使用标签和全部。 我已经用另一个答案了帖子也是如此。请检查链接..并做 停止分配标签并使用它来识别你的按钮并实现 按钮c舔 https://stackoverflow.com/a/48882197/9130596

登录后方可回帖

Loading...