在“按钮”操作中访问Tableview单元格

时间:2019-10-13 19:41:19

标签: swift uitableview animation

我想通过单击同一单元格中存在的按钮来删除tableview单元格。但是我无法访问按钮动作功能中的单元格。 请帮助我访问此单元格。我的代码是-

class MatchesViewController: UIViewController{

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    guard let cell = tableView.dequeueReusableCell(withIdentifier: "MatchingUsersTVCell") as? MatchingUsersTVCell else{
        return UITableViewCell()
    }
    let likeUid = userIdArray[indexPath.row]

    cell.heartBtn.tag = indexPath.row

    cell.heartBtn.addTarget(self, action: #selector(userLikeButtonWasTappaed(sender:)), for: .touchUpInside)
    }

    @objc func userLikeButtonWasTappaed(sender: UIButton){
        if let cell = sender.superview as? MatchingUsersTVCell{
            CellAnimator.animate(cell: cell)
        }
        let tag = sender.tag
        let userid = userIdArray[tag]
    }
}

4 个答案:

答案 0 :(得分:0)

MatchingUsersTVCell内,添加两个属性,一个属性为parentVC,名称为UIViewController,另一个属性为index,名称为Int

class MatchingUsersTVCell: UITableViewCell {
    var parentVC: UIViewController!
    var index: Int!

    ...

}

然后,在创建每个单元格时,适当设置以下两个值:

class MatchesViewController: UIViewController, UITableViewDelegate, UITableViewDatasource {

    ...

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "MatchingUsersTVCell") as? MatchingUsersTVCell else {
            return UITableViewCell()
        }
        cell.parentVC = self
        cell.index = index

        ...

        return cell
    }
}

现在,您只需在点击按钮时更新parentVC的tableView的数据源并重新加载其数据:

class MatchingUsersTVCell: UITableViewCell {

    ...

    @objc func userLikeButtonWasTappaed(sender: UIButton){
        parentVC.userIdArray.remove(at: index)
        parentVC.tableView.reloadData()
    }

}

答案 1 :(得分:0)

我将不使用标签,而是实现协议/委托。 使用indexPath可以使用多个部分,等等。

1)创建协议:

protocol MatchingUsersTVCellDelegate : class {
    func didTapLikeButton(_ indexPath: IndexPath)
    func didTapOtherButton(_ indexPath: IndexPath)
}

2)创建/更新您的单元格:

class MatchingUsersTVCell : UITableViewCell {
    weak var delegate: MatchingUsersTVCellDelegate?   
    var indexPath: IndexPath!
    // add target to your like button
    func didTapLIkeButton(_ sender: UIButton) {
        self.delegate?.didTapLikeButton(indexPath)
    }
    func didTapOtherButton() {
        self.delegate?.didTapOtherButton(indexPath)
    }
}

3)确保您的viewController符合新的委托:

extension YourViewController: MatchingUsersTVCellDelegate {
    func didTapLikeButton(_ indexPath: IndexPath) {
        //Do something with the indexPath or indexPath.row
        dataSource.remove(at: indexPath.row)
    }
    func didTapOtherButton(_ indexPath: IndexPath) {
        //Do something else with the indexPath or indexPath.row
    }
}

4)设置委托和indexPath

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell...
    cell.delegate = self
    cell.indexPath = indexPath
    return cell
}

答案 2 :(得分:0)

您可以在选择器方法中获得它

private void addActionItem(@NonNull LayoutInflater inflater, int index,
    @NonNull final FABAction item) {
    // Inflate & Configure item
    final View subItem = inflater.inflate(R.layout.fab_subitem, mItemContainer, false);
    ImageView image = ButterKnife.findById(subItem, R.id.fab_subitem_image); <--This is old code

    image.setBackgroundColor(item.mBgColor);

答案 3 :(得分:0)

尝试以下代码:

@objc func userLikeButtonWasTappaed(sender: UIButton){

    guard let indexPath = tableView.indexPathForRow(at: sender.convert(sender.frame.origin, to: tableView)) else {
        return
    }

    let cell = tableView.cellForRow(at: indexPath) as? MatchingUsersTVCell
}

然后在您的cellForRowAt函数中添加以下代码:

cell.yourBtn.tag = indexPath.row

        cell.yourBtn.addTarget(self, action: #selector(userLikeButtonWasTappaed(sender:)), for: .touchUpInside)
相关问题