Swift 使用 Table 查看单元格数据传递技术的段控制

Swift Segment control with Table View Cell Data Passing techniques

我想将数据从一个 table 视图单元格传递到另一个 table 视图单元格。当用户单击添加按钮时,我想将那些 table 视图单元格值添加到另一个段控件。我在同一个视图控制器中有两个 table 视图和单元格。我正在跟随委托人实施它,但我遇到了一些问题。

1.我使用 isHide 方法根据段控制选择隐藏特定 table 视图,仍然显示两个 table 视图,尽管我想显示一个和两个段显示相同的数据。

这是代码。

@IBAction func selectSegment(_ sender: UISegmentedControl) {
        
       if sender.selectedSegmentIndex == 0{
           FavouriteTableView.isHidden = true
        setUpUI()
        presenter = MoviePresenter(view: self)
        presenter.getMovies()
           
        }
        
    } 

2。当用户单击按钮但实际上不起作用时,我想将 table 视图单元格值添加到秒 table 视图单元格。这是两个 table 视图单元格的代码。

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
        
        movieImage.image = nil
        if let imageData = data{
            movieImage.image = UIImage(data: imageData)
           //  movieImage.image = nil
        }
    }
    
}

第二个单元格的代码。

protocol CellDelegate: AnyObject {
    func AddFavourite(cell: FavouriteTableViewCell)
}

class FavouriteTableViewCell: UITableViewCell {
    
    weak var delegate:CellDelegate?

    @IBOutlet weak var FavouriteImage: UIImageView!
    
    @IBOutlet weak var FavouritemovieTitle: UILabel!
    
    @IBOutlet weak var FavouritemovieOverview: UILabel!
    
    @IBAction func NewMovie(_ sender: UIButton) {
        self.delegate?.AddFavourite(cell: self)
    }
    
}

Table 查看控​​制器代码实现..

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

       userName.text = "Hello: " + finalname

       
    }
    private func setUpUI() {
        tableView.dataSource = self
        tableView.delegate = self
    }
    
    
   private func searchBarText() {
        searchBar.delegate = self
    }
    
    @IBAction func selectSegment(_ sender: UISegmentedControl) {
        
       if sender.selectedSegmentIndex == 0{
           FavouriteTableView.isHidden = true
        setUpUI()
        presenter = MoviePresenter(view: self)
        presenter.getMovies()
           
        }
        
    }

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 baseImageURL = presenter.getUrlImage(by: row)
        let data = presenter.getImageData(by: baseImageURL)
        cell.delegate = self
        cell.configureCell(title: title, overview: overview, data: data)
        return cell
        
    }
    

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 = cell.movieTitle.text ?? ""
        customViewController?.imagemovie = cell.movieImage.image
        customViewController?.overview = cell.movieOverview.text ?? ""
        self.navigationController?.pushViewController(customViewController!, animated: true)
                    
                   
    }
}
extension MovieViewController :  CellDelegate{
    func AddFavourite(cell: FavouriteTableViewCell) {
        guard (self.tableView.indexPath(for: cell) != nil) else {return}
        
        movieTitle = cell.FavouritemovieTitle.text ?? ""
        movieOverview = cell.FavouritemovieOverview.text ?? ""
        movieImage =  cell.FavouriteImage.image
    }
    
    
    
}

此处为应用设计截图。

这是应用程序的屏幕截图。

在你的代码中,首先你需要最喜欢的电影的模型,而不是将它保存在全局存储中,就像这个:

struct Movie {
    let title: String
    let overview: String
    let image: UIImage?
}

将您的阵列模型添加到您的控制器中:

// append to this array when user taped on favorite button
var favorites = [Movie]()

在 tableViewDelegate 中将每个 tableView 分开(这种情况下最好只使用一个 tableView):

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      if tableView == FavoriteTableView {
        return favorites.count
      } else {
        return presenter.rows
      }
 }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView == FavoriteTableView {
            let cell = tableView.dequeueReusableCell(withIdentifier: FavouriteTableViewCell, for: indexPath) as! FavouriteTableViewCell
            cell.delegate = self // This cause favorite protocol method trigered
            return cell
        } else {
            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 baseImageURL = presenter.getUrlImage(by: row)
            let data = presenter.getImageData(by: baseImageURL)
            cell.delegate = self
            cell.configureCell(title: title, overview: overview, data: data)
            return cell
        }
    }

并在需要时像这样更新其他委托方法。