gpt4 book ai didi

ios - 当数据源和委托(delegate)与 View Controller 分离时,如何将 TableView 的单击项目发送到其他 View Controller

转载 作者:行者123 更新时间:2023-11-28 15:20:12 25 4
gpt4 key购买 nike

我是 iOS 的初学者,来自 android 背景,刚刚了解了表格 View (对我来说是 Android ListView)。我正在尝试将数据源和委托(delegate)与 View Controller 分开。我找到了一些关于如何执行此操作的教程,但一直在弄清楚如何将单击的项目发送到另一个 View Controller 。代码如下:

class PictureTableViewController: UIViewController {


@IBOutlet weak var pictureTableView: UITableView!

private let picsDataSource: PicturesDataSource

required init?(coder aDecoder: NSCoder) {
self.picsDataSource = PicturesDataSource()
super.init(coder: aDecoder)
}

override func viewDidLoad() {
super.viewDidLoad()

pictureTableView.dataSource = picsDataSource
pictureTableView.reloadData()
pictureTableView.delegate = picsDataSource
}
}

class PicturesDataSource: NSObject, UITableViewDataSource, UITableViewDelegate{

private var pictureModels = [PictureModel]()

override init(){
let picModelsDataController = PictureModelsDataController()
pictureModels = picModelsDataController.pictureModels
}


func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pictureModels.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: PictureCell.self)) as! PictureCell

let picModel = pictureModels[indexPath.row]
cell.pictureName = picModel.pictureName
cell.imageItem = picModel.imageItem

return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


//1 - try loading the "Detail" view controller and typecasting it to be DetailViewController
if let detailViewController = storyboard.instantiateViewController(withIdentifier: "PictureDetailView") as? PictureDetailViewController {

//2 - success! set its selecteImage property
detailViewController.selectedImgName = pictureModels[indexPath.row].pictureName

//3 - now push it onto the navigation controller
navigationController?.pushViewController(detailViewController, animated: true)
}
}

}

错误:func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ }由于“storyboard”和“navigationController”在 PicturesDataSource 类中不可用,我如何将点击的项目(图片名称)发送到 DetailsViewController

有关于分离数据源和委托(delegate)的 StackOverflow 答案,但没有解决我的问题。

Using: Xcode 8.3 beta 6

最佳答案

您可以在 TableView 事件处理程序中包含对主视图 Controller 的引用。下面是我从您的示例中派生的 Playground 代码:

import UIKit

// MARK: - Model

struct Picture {
let title: String
let image: UIImage
}

struct PictureModelsDataSource {
let pictures = [
Picture(title: "exampleTitle", image: UIImage(named: "exampleImage")!),
Picture(title: "exampleTitle", image: UIImage(named: "exampleImage")!)
]
}

// MARK - View

class PictureCell: UITableViewCell {
@IBOutlet weak var pictureTitleLabel: UILabel!
@IBOutlet weak var pictureImage: UIImageView!
}

// MARK: - Controller

class PictureTableViewController: UIViewController {

// MARK: - Properties

@IBOutlet weak var pictureTableView: UITableView!

private var pictureListController: PictureListController?

// MARK: - View lifecycle

override func viewDidLoad() {
super.viewDidLoad()
pictureListController = PictureListController()
pictureListController?.viewController = self
pictureTableView.dataSource = pictureListController
pictureTableView.delegate = pictureListController
pictureTableView.reloadData()
}
}

class PictureDetailViewController: UIViewController {
var selectedPictureTitle: String?
}

class PictureListController: NSObject, UITableViewDataSource, UITableViewDelegate {

// MARK: - Properties

weak var viewController: PictureTableViewController?

private let pictures: [Picture] = {
let pictureModelsDataSource = PictureModelsDataSource()
return pictureModelsDataSource.pictures
}()

// MARK: - View setup

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

// MARK: - Event handling

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pictures.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: PictureCell.self)) as? PictureCell else {
return UITableViewCell()
}

let picture = pictures[indexPath.row]
cell.pictureTitleLabel.text = picture.title
cell.pictureImage.image = picture.image

return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let pictureTitle = pictures[indexPath.row].title
let storyboard = UIStoryboard(name: "exampleStoryboard", bundle: nil)
if let pictureDetailViewController = storyboard.instantiateViewController(withIdentifier: "PictureDetailView") as? PictureDetailViewController {
pictureDetailViewController.selectedPictureTitle = pictureTitle
viewController?.navigationController?.pushViewController(pictureDetailViewController, animated: true)
}
}
}

关于ios - 当数据源和委托(delegate)与 View Controller 分离时,如何将 TableView 的单击项目发送到其他 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46140943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com