单元格按钮 tableView Swift

发布于 2025-01-18 02:06:52 字数 1365 浏览 1 评论 0原文

我有一个带有部分的桌面视图,每个部分都会添加您添加的待办事项。 我正在尝试在图片中的图片上进行动画,以便使用单元按钮委托功能单击。但是我真的不知道如何仅连接到那一件项目并使其动画。 我试图访问的内容与我在didSelectrow中的单元格相同,但是在此CellButtonFunction中,因为这是我要按下的单元格中的按钮,而不是整个单元格。

我想在按钮下完成的事情就像以下示例:

cellpressed.image.ishidden = false cellpressed.image.himate()

此单元格图像具有一个动画类,可以设置为它,这只是我需要击中该

观看的一些YouTube视频并进行了研究,但我不工作,所以希望你们能


extension ViewController: FirstTableViewCellDelegate {
    func myImageButtonTapped(with index: IndexPath, view: AnimatedCheckmarkView) {
        HapticsManager.shared.selectionVibrate()
        let todo = self.tableViewCoreData[index.section].sortedItems[index.row - 1]

        todo.isMarked = !todo.isMarked
        
        if todo.isMarked == true {
            self.tableViewCoreData[index.section].totalMarked += 1
            view.animate(withDuration: 1.0, delay: 0.7)
            view.isHidden = false
            do {
               try self.context.save()
            }
            catch{
                //error
            }
            tableView.reloadData()
        } else {
            view.isHidden = true
            self.tableViewCoreData[index.section].totalMarked -= 1
            do {
               try self.context.save()
            }
            catch{
                //error
            }
            tableView.reloadData()
        }

在这里帮助我。除了视图。

I have a tableView with Sections and every section got todoitems that u add.
I'm trying to animate in my picture on the todoItem I click with my cell button delegate function. But I really don't know how to connect to just that one item and make it animate.
What I'm trying to get access to is the same access to a cell like I do in didSelectRow but in this cellButtonFunction, since it's the button in the cell I'm gonna press on and not the whole cell.

The thing I wanna accomplish with pressing the button is like this example:

cellPressed.image.ishidden = false
cellPressed.image.animate()

This cell image have an animated class that works set to it, it's just that I need to hit the specific cell for this

Watched a few YouTube videos and researched but I don't get it to work, so I hope you guys can help me


extension ViewController: FirstTableViewCellDelegate {
    func myImageButtonTapped(with index: IndexPath, view: AnimatedCheckmarkView) {
        HapticsManager.shared.selectionVibrate()
        let todo = self.tableViewCoreData[index.section].sortedItems[index.row - 1]

        todo.isMarked = !todo.isMarked
        
        if todo.isMarked == true {
            self.tableViewCoreData[index.section].totalMarked += 1
            view.animate(withDuration: 1.0, delay: 0.7)
            view.isHidden = false
            do {
               try self.context.save()
            }
            catch{
                //error
            }
            tableView.reloadData()
        } else {
            view.isHidden = true
            self.tableViewCoreData[index.section].totalMarked -= 1
            do {
               try self.context.save()
            }
            catch{
                //error
            }
            tableView.reloadData()
        }

Here is the function. Litterly everything in this works except the view.animate() that animates one random view instead

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天旳寂寞 2025-01-25 02:06:52

首先创建代表以从单元类中的单元格中接收动作,

protocol CellDelegate:AnyObject{
 /// pass arguments as per requirement 
 func cell(buttonDidPressed indexPath: IndexPath)
}

创建弱参考代表的参考,

class Cell: UICollectionViewCell{
 weak var delegate: CellDelegate? = nil
 var indexPath: IndexPath!

/// code 

 @IBAction func buttonDidPressed(_ sender: UIButton) {
  delegate?.cell(buttonDidPressed: indexPath)
 }
}

现在在ur Controller中

extension ViewController: UICollectionViewDelegate,
                          UICollectionViewDataSource,
                          CellDelegate{
   
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        // let cell : Cell dequeueReusableCell your cell
        cell.delegate = self
        cell.indexPath = indexPath
        return cell
    }

    func cell(buttonDidPressed indexPath: IndexPath){
     // your task on cell click
    }

}

请确认此协议您还可以在Cell类中使用协议的替代方案。

First create delegate to receive action from cell

protocol CellDelegate:AnyObject{
 /// pass arguments as per requirement 
 func cell(buttonDidPressed indexPath: IndexPath)
}

Now in your Cell class create weak reference of delegate

class Cell: UICollectionViewCell{
 weak var delegate: CellDelegate? = nil
 var indexPath: IndexPath!

/// code 

 @IBAction func buttonDidPressed(_ sender: UIButton) {
  delegate?.cell(buttonDidPressed: indexPath)
 }
}

now in ur controller confirm this protocol

extension ViewController: UICollectionViewDelegate,
                          UICollectionViewDataSource,
                          CellDelegate{
   
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        // let cell : Cell dequeueReusableCell your cell
        cell.delegate = self
        cell.indexPath = indexPath
        return cell
    }

    func cell(buttonDidPressed indexPath: IndexPath){
     // your task on cell click
    }

}

You can also use closures in cell class alternative to protocol

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文