将数据从 Table 视图单元格传递到 Segment 控件
Passing data to Segment control from Table view Cell
我正在使用 table 视图单元格来显示数据。我有一个段控制。在第一部分中,我显示了带有按钮控件的移动列表。 当用户单击复选标记按钮时,我想将那些 table 视图单元格值发送到秒控件并添加到它上面 .. 请给我一些基于下面的代码..
这是 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)
}
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
}
}
}
Table 查看行代码的单元格 ..
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 ?? ""
// customViewController?.movieTitleHeader.text = cell.movieTitle.text ?? ""
self.navigationController?.pushViewController(customViewController!, animated: true)
}
}
是否实施了select方法..
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) ?? ""
let baseImageURL = presenter.getUrlImage(by: row)
dc.imagemovie = UIImage(data: presenter.getImageData(by: baseImageURL)!)
self.navigationController?.pushViewController(dc, animated: true)
}
}
这是应用程序的屏幕截图。
当用户单击复选框时,我想将那些 table 查看单元格值添加到收藏夹控件中
我是你需要实现的例子(方法名和变量名不完全一样,只是帮助你理解原理)。在这种情况下只有一个 table 视图:
struct MoviePresenter {
var nbRows: Int = 0
}
class MoviesVC {
var presenter = MoviePresenter()
var tableViewSelect : UISegmentedControl!
var movieTableView: UITableView!
var favoriteMovies = [Int]() // the list of row of favorite movies
// utility function to know if movie is favorite or not
func isInFavorites(_ row: Int) -> Bool {
return favoriteMovies.first(where: {[=10=] == row}) != nil
}
// Note : all table view datasource/delegate methods need to check
// which is the current selected display
func numberOfRowsInSection(_ section: Int) -> Int {
if tableViewSelect.selectedSegmentIndex == 0 {
return presenter.nbRows
} else {
return favoriteMovies.count
}
}
// in cellForRow
func cellForRow(indexPath: IndexPath) {
if tableViewSelect.selectedSegmentIndex == 0 {
// default display
let cell = UITableViewCell() // you dequeue your cell here
// init cell for presenter (by: row)
if isInFavorites(indexPath.row) {
// checkMark handling
}
// to handle checkmark
cell.tag = indexPath.row
} else {
// favorite list
let cell = UITableViewCell() // you dequeue your favorite cell here
let row = favoriteMovies[indexPath.row]
// if you want to handle checkmark
cell.tag = row
}
}
func checkMarkTappedInFavoriteCell(cell: UITableViewCell) {
let row = cell.tag
// set/unset favorite check mark when tapped
if isInFavorites(row) {
favoriteMovies.removeAll(where: {[=10=] == row})
} else {
favoriteMovies.append(row)
}
movieTableView.reloadRows(at: [IndexPath(row: row, section: 0)], with: .none)
}
// action to be executed when selected segment changes
@IBAction func tableViewSelectedIndexChanged(_ sender: UISegmentedControl) {
self.movieTableView.reloadData()
}
}
这仅适用于您只需要一个 table 视图的情况。如果您需要 2 个 table 视图,分段控件中的更改也会使一个 table 视图可见,另一个不可见。
我正在使用 table 视图单元格来显示数据。我有一个段控制。在第一部分中,我显示了带有按钮控件的移动列表。 当用户单击复选标记按钮时,我想将那些 table 视图单元格值发送到秒控件并添加到它上面 .. 请给我一些基于下面的代码.. 这是 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)
}
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
}
}
}
Table 查看行代码的单元格 ..
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 ?? ""
// customViewController?.movieTitleHeader.text = cell.movieTitle.text ?? ""
self.navigationController?.pushViewController(customViewController!, animated: true)
}
}
是否实施了select方法..
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) ?? ""
let baseImageURL = presenter.getUrlImage(by: row)
dc.imagemovie = UIImage(data: presenter.getImageData(by: baseImageURL)!)
self.navigationController?.pushViewController(dc, animated: true)
}
}
这是应用程序的屏幕截图。
当用户单击复选框时,我想将那些 table 查看单元格值添加到收藏夹控件中
我是你需要实现的例子(方法名和变量名不完全一样,只是帮助你理解原理)。在这种情况下只有一个 table 视图:
struct MoviePresenter {
var nbRows: Int = 0
}
class MoviesVC {
var presenter = MoviePresenter()
var tableViewSelect : UISegmentedControl!
var movieTableView: UITableView!
var favoriteMovies = [Int]() // the list of row of favorite movies
// utility function to know if movie is favorite or not
func isInFavorites(_ row: Int) -> Bool {
return favoriteMovies.first(where: {[=10=] == row}) != nil
}
// Note : all table view datasource/delegate methods need to check
// which is the current selected display
func numberOfRowsInSection(_ section: Int) -> Int {
if tableViewSelect.selectedSegmentIndex == 0 {
return presenter.nbRows
} else {
return favoriteMovies.count
}
}
// in cellForRow
func cellForRow(indexPath: IndexPath) {
if tableViewSelect.selectedSegmentIndex == 0 {
// default display
let cell = UITableViewCell() // you dequeue your cell here
// init cell for presenter (by: row)
if isInFavorites(indexPath.row) {
// checkMark handling
}
// to handle checkmark
cell.tag = indexPath.row
} else {
// favorite list
let cell = UITableViewCell() // you dequeue your favorite cell here
let row = favoriteMovies[indexPath.row]
// if you want to handle checkmark
cell.tag = row
}
}
func checkMarkTappedInFavoriteCell(cell: UITableViewCell) {
let row = cell.tag
// set/unset favorite check mark when tapped
if isInFavorites(row) {
favoriteMovies.removeAll(where: {[=10=] == row})
} else {
favoriteMovies.append(row)
}
movieTableView.reloadRows(at: [IndexPath(row: row, section: 0)], with: .none)
}
// action to be executed when selected segment changes
@IBAction func tableViewSelectedIndexChanged(_ sender: UISegmentedControl) {
self.movieTableView.reloadData()
}
}
这仅适用于您只需要一个 table 视图的情况。如果您需要 2 个 table 视图,分段控件中的更改也会使一个 table 视图可见,另一个不可见。