Delegate Method to UItableViewCell Swift

前端 未结 3 712
Happy的楠姐
Happy的楠姐 2021-01-25 03:11

I have a Social Network Feed in form UItableView which has a cell. Now each cell has an image that animates when an even is triggered. Now, This event is in form of a string, wi

相关标签:
3条回答
  • 2021-01-25 03:42

    If I correctly understood your question, maybe this could help:

    class ViewController: UIViewController, YourCustomTableDelegate {
    
    @IBOutlet weak var tableView: YourCustomTableView!  
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.tableView.customTableDelegate = self
        }
    
    
        // table delegate method
        func shouldAnimateCell(at indexPath: IndexPath) {
           if let cell = tableView.cellForRow(at: indexPath) {
               cell.animate(...)
           }    
        }
    }
    
    0 讨论(0)
  • 2021-01-25 03:45

    If I understand you correctly, you are trying to make each of your cells conform to a protocol that belongs to their UITableView? If this is the case then this cannot be done. The Delegation design pattern is a one to one relationship, i.e only one of your UITableViewCells would be able to conform to the UITableView's delegate.

    Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled. The delegate may respond to the message by updating the appearance or state of itself or other objects in the application, and in some cases it can return a value that affects how an impending event is handled. The main value of delegation is that it allows you to easily customize the behavior of several objects in one central object.

    Quote from the Apple Docs

    I would suggest that your UITableViewCell should call a block (Objective-C) or a closure (Swift) whenever your specified event is triggered to achieve what you are looking for. Set up this closure in your tableView:cellForRowAtIndexPath function.

    EXAMPLE

    TableViewController

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
      {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCellID", for: indexPath) as! MyTableViewCell
        cell.eventClosure = {
         //Do something once the event has been triggered.
        }
        return cell
      }
    

    TableViewCell

    func eventTriggered()
    {
      //Call the closure now we have a triggered event.
      eventClosure()
    }
    
    0 讨论(0)
  • 2021-01-25 03:48

    Try something like this:

    Define your delegate protocol:

    protocol CustomCellDelegate: class {
        func animationStarted()
        func animationFinished()
    }
    

    Define your CustomCell. Extremely important to define a weak delegate reference, so your classes won't retain each other.

    class CustomCell: UITableViewCell {
        // Don't unwrap in case the cell is enqueued!
        weak var delegate: CustomCellDelegate?
    
        /* Some initialization of the cell */
    
        func performAnimation() {
            delegate?.animationStarted()
            UIView.animate(withDuration: 0.5, animations: {
                /* Do some cool animation */
            }) { finished in
                self.delegate?.animationFinished()
            }
        }
    }
    

    Define your view controller. assign delegate inside tableView:cellForRowAt.

    class ViewController: UITableViewDelegate, UITableViewDataSource {
    
        /* Some view controller customization */
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: CustomCell.self)) as? CustomCell
            cell.delegate = self
            cell.performAnimation()
            return cell
        }
    }
    
    0 讨论(0)
提交回复
热议问题