使用按钮委托从 Table 视图单元格传递数据

Passing data from Table view cell using button delegate

我想在用户单击按钮时将数据从一个视图控制器传递到另一个视图控制器。我正在使用带委托的按钮将 table 视图单元格值传递到不同的视图控制器视图中。在第二个视图控制器中,我有两个标签和一个图像来显示字段,但问题是当我单击按钮时它是空的。

这是手机号码。

import UIKit



protocol CellSubclassDelegate: AnyObject {
    func buttonTapped(cell: MovieViewCell)
}

class MovieViewCell: UITableViewCell {
    
    weak var delegate:CellSubclassDelegate?
    
    static let identifier = "MovieViewCell"
    

    @IBOutlet weak var movieImage: UIImageView!
    @IBOutlet weak var movieTitle: UILabel!
    @IBOutlet weak var movieOverview: UILabel!
    
    @IBOutlet weak var someButton: UIButton!
    
    @IBAction func someButtonTapped(_ sender: UIButton) {
        self.delegate?.buttonTapped(cell: self)
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        self.delegate = nil
    }
    
    func configureCell(title: String?, overview: String?, data: Data?) {
        
        movieTitle.text = title
        movieOverview.text = overview
        
        if let imageData = data{
            movieImage.image = UIImage(data: imageData)
           //  movieImage.image = nil
        }
    }
    
}

这是第一个视图控制器代码。

import UIKit

class MovieViewController: UIViewController, UISearchBarDelegate {
    
    @IBOutlet weak var userName: UILabel!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var searchBar: UISearchBar!
    
    
    
    private var presenter: MoviePresenter!
    
    var finalname = ""
    
   var movieTitle = ""
    var movieOverview = ""
    var movieImage : UIImage?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

       userName.text = "Hello: " + finalname
       setUpUI()
      presenter = MoviePresenter(view: self)
       searchBarText()
       
    }
    private func setUpUI() {
        tableView.dataSource = self
        tableView.delegate = self
    }

    
    
    
   private func searchBarText() {
        searchBar.delegate = self
    }
    
    @IBAction func selectSegment(_ sender: UISegmentedControl) {
        
        if sender.selectedSegmentIndex == 0{
         setUpUI()
        presenter = MoviePresenter(view: self)
        presenter.getMovies()
        }
    }
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if searchText == ""{
            presenter.getMovies()
        }
        else {
            presenter.movies = presenter.movies.filter({ movies in
                    let originalTitle = movies.originalTitle.lowercased().range(of: searchText.lowercased())
                    let overview = movies.overview.lowercased().range(of: searchText.lowercased())
                    let posterPath = movies.posterPath.lowercased().range(of: searchText.lowercased())
                return (originalTitle != nil) == true || (overview != nil) == true || (posterPath != nil) == true}
                )
        }
        
        tableView.reloadData()
    }
   
    
}

extension MovieViewController: MovieViewProtocol {
    
    func resfreshTableView() {
        tableView.reloadData()
    }
    
    func displayError(_ message: String) {
        let alert = UIAlertController(title: "Error", message: message, preferredStyle: .alert)
        let doneButton = UIAlertAction(title: "Done", style: .default, handler: nil)
        alert.addAction(doneButton)
        present(alert, animated: true, completion: nil)
    }
    
}

extension MovieViewController: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        presenter.rows
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: MovieViewCell.identifier, for: indexPath) as! MovieViewCell
        
        let row = indexPath.row
        let title = presenter.getTitle(by: row)
        let overview = presenter.getOverview(by: row)
        let data = presenter.getImageData(by: row)
        cell.delegate = self
        cell.configureCell(title: title, overview: overview, data: data)
        
       
        
        return cell
    }
    
   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let dc = storyboard?.instantiateViewController(withIdentifier: "MovieDeatilsViewController") as! MovieDeatilsViewController
        
        let row = indexPath.row
       dc.titlemovie = presenter.getTitle(by: row) ?? ""
       dc.overview = presenter.getOverview(by: row) ?? ""
       dc.imagemovie = UIImage(data: presenter.getImageData(by: row)!)
        self.navigationController?.pushViewController(dc, animated: true)
   }
    
    
}


extension MovieViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
}

extension MovieViewController : CellSubclassDelegate{
    func buttonTapped(cell: MovieViewCell) {
        guard (self.tableView.indexPath(for: cell) != nil) else {return}
            let customViewController = storyboard?.instantiateViewController(withIdentifier: "MovieDeatilsViewController") as? MovieDeatilsViewController
            customViewController?.titlemovie = movieTitle
            customViewController?.imagemovie = movieImage
            customViewController?.overview = movieOverview
        self.navigationController?.pushViewController(customViewController!, animated: true)
                    
                   
    }
    }
 

这是详细视图控制器代码。

class MovieDeatilsViewController: UIViewController {
    
    
    @IBOutlet weak var movieImage: UIImageView!
    
    @IBOutlet weak var movieTitle: UILabel!
    
    @IBOutlet weak var movieOverview: UILabel!
    
    
    var titlemovie = ""
    var overview = ""
    var imagemovie :UIImage?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        movieTitle.text = titlemovie
        movieOverview.text = overview
        movieImage.image = imagemovie
        
    
        
    }

}

这是我点击按钮时的结果。

问题是您在选择每一行时没有更新您的全局属性,
如果您通过单元格委托传递数据并通过委托传递您的单元格,则可以从单元格传递数据,例如:

    customViewController?.titlemovie = cell.movieTitle.text ?? ""
    customViewController?.imagemovie = cell.movieImage.image
    customViewController?.overview = cell.movieOverview.text ?? ""

当然最好将您的数据模型传递给您的单元格。然后通过你的代表分享,而不是分享你的手机,比如:

protocol CellSubclassDelegate: AnyObject {
    func buttonTapped(cell: MovieModel)
}